@fraczak/k 8.0.0 → 8.1.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.
@@ -10,16 +10,10 @@ export const vfsData = {
10
10
  "Examples/poly.k": "-- Examples/poly.k: Polymorphic list functions in k\n-- Uses arbitrary-precision integers from Examples/arithmetics.k.\n\n-- List constructors and destructors\nlist[X] = < {} nil, { X car, Y cdr } cons > = Y;\nlist? = ? list[X];\nnil = {} | nil list?;\nsingleton = {() car, nil cdr} | cons list?;\ncons = {.car car, .cdr cdr} | cons list?;\nnil? = list? / nil nil;\ncar = list? /cons .car;\ncdr = list? /cons .cdr;\n\n-- Decrement an integer and normalize (stripping leading zeros)\npred_int = dec /'+' nat | '+';\n\n-- Concatenate two lists: {xs, ys} -> list\nconcat = {.xs reverse xs, .ys acc} _reverse;\n\n-- Reverse a list: xs -> list\nreverse = {() xs, nil acc} _reverse;\n_reverse = <\n {.xs cdr xs, {.xs car car, .acc cdr} cons acc} _reverse,\n .acc\n>;\n\n-- Length of a list: xs -> int\nlength = {() xs, 0 int acc} _length;\n_length = <\n {.xs /nil if, .acc then} .then,\n {.xs cdr xs, .acc inc acc} _length\n>;\n\n-- Get N-th element of a list (0-indexed): {n, xs} -> element\nget_nth = <\n {.n zero_int? if, .xs car then} .then,\n {.n pred_int n, .xs cdr xs} get_nth\n>;\n\n-- Split list at index N: {n, xs} -> {first, second}\nsplit_by = {.n n, .xs xs, nil acc} _split_by;\n_split_by = <\n {.n zero_int? if, {.acc reverse first, .xs second} then} .then,\n {.xs /nil if, {.acc reverse first, nil second} then} .then,\n {.n pred_int n, .xs cdr xs, {.xs car car, .acc cdr} cons acc} _split_by\n>;\n\n-- Take the first N elements of a list: {n, xs} -> list\ntake = split_by .first;\n\n-- Drop the first N elements of a list: {n, xs} -> list\ndrop = <\n {.n zero_int? if, .xs then} .then,\n {.xs /nil if, nil then} .then,\n {.n pred_int n, .xs cdr xs} drop\n>;\n\n-- Repeat element X, N times: {n, x} -> list\nrepeat = {.n n, .x x, nil acc} _repeat;\n_repeat = <\n {.n zero_int? if, .acc then} .then,\n {.n pred_int n, .x x, {.x car, .acc cdr} cons acc} _repeat\n>;\n\n-- Append an element to the end of a list: {x, xs} -> list\nappend = {.xs xs, {.x car, nil cdr} cons ys} concat;\n\n-- Zip two lists into a list of {x, y} pairs: {xs, ys} -> list\nzip = <\n {.xs /nil if, nil then} .then,\n {.ys /nil if, nil then} .then,\n {\n {.xs car x, .ys car y} car,\n {.xs cdr xs, .ys cdr ys} zip cdr\n } cons\n>;\n\n-- Gen list of intgers: n -> [0, 1, ..., n]\ngen_list = {() n, nil list} gen_list_; \ngen_list_ = <\n {.n neg? if, .list then} .then, \n {.n dec n, {.n car, .list cdr} cons list} gen_list_\n>;\n\n-- Identity filter on {x, y} when x is greater than y: ?{X x, X y}=Y -> ?Y\ngt? =\n <\n {.x/- if1, .y/+ if2, () then} .then,\n {{.x/- y, .y/- x} GT if, () then} .then,\n {{.x/+ x, .y/+ y} GT if, () then} .then\n >\n;\n\n-- Split list into two halves by alternating elements:\n-- xs -> {first, second}\nsplit = {() xs, nil first, nil second} _split;\n_split = <\n {.xs /nil if, {.first first, .second second} then} .then,\n {\n .xs cdr /nil if,\n {{.xs car car, .first cdr} cons first, .second second} then\n } .then,\n {\n .xs cdr cdr xs,\n {.xs car car, .first cdr} cons first,\n {.xs cdr car car, .second cdr} cons second\n } _split\n>;\n\n-- Merge two sorted lists: {xs, ys} -> list\nmerge = <\n {.xs /nil if, .ys then} .then,\n {.ys /nil if, .xs then} .then,\n {\n {.xs car x, .ys car y} gt? if,\n {.ys car car, {.xs xs, .ys cdr ys} merge cdr} cons then\n } .then,\n {.xs car car, {.xs cdr xs, .ys ys} merge cdr} cons\n>;\n\n-- Sort a list in ascending order (merge sort): xs -> list\nsort = list? <\n /nil nil,\n /cons <\n {.cdr /nil if, .car singleton then} .then,\n |cons split {.first sort xs, .second sort ys} merge\n >\n> list?;\n\n\n",
11
11
  "karatsuba-mult.k": "-- Karatsuba multiplication O(n^1.585)\n-- Self-contained and minimal implementation\n-- for arbitrary-precision integers\n\nyes = {} | yes;\nno = {} | no;\n\nbits[] = < {} _, B 0, B 1 > = B;\nbits? = ? bits[];\n0b = {} | _;\n0 = 0b |0;\n1 = 0b |1;\n\nunat[] = < {} _, U 1 > = U;\nunat? = ? unat[];\n0u = {} | _;\n\nempty = < /_ yes, no >;\nempty? = {empty /yes if, () then} .then;\n\ninv = {()x, 0b r} _inv;\n_inv = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .r then} .then,\n <\n {.x /0 x, .r |0 r},\n {.x /1 x, .r |1 r}\n > _inv\n>;\n\nconcat = {.x inv x, .y y} concat_;\nconcat_ = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .y then} .then,\n <\n {.x /0 x, .y |0 y},\n {.x /1 x, .y |1 y}\n > concat_\n>;\n\nremove_leading_zeros = < /0 remove_leading_zeros, () >;\nnat = remove_leading_zeros <empty? 0, ()>;\n\nint = nat | '+';\nabs = </'+', /'-'>;\n\nsign_plus = {} | '+';\nsign_minus = {} | '-';\nswap_sign = < /'+' | '-', /'-' | '+' >;\n\nc0 = {} | 0;\nc1 = {} | 1;\n\n_plus = {.x x, .y y, .c c, 0b r} _plus_;\n_plus_ = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,\n {.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,\n {.x empty? if, .c/0 and, {.r x, .y r} _inv then} .then,\n <\n {.x y, .y empty? x, .c c, .r r},\n {<.c/0 0, 1> x, .y y, .x empty? c0 c, .r r},\n {.x /0 x, .y /0 y, .c/0 c0 c, .r |0 r},\n {.x /0 x, .y /0 y, .c/1 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/0 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /0 y, .c/0 c0 c, .r |1 r},\n {.x /1 x, .y /0 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}\n > _plus_\n>;\n\n_minus = {.x x, .y y, .c c, 0b r} _minus_;\n_minus_ = ?{ bits[] = X x, ... }\n<\n {.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,\n <\n {.x/0 x, .y empty? y, .c/1 c1 c, .r |1 r},\n {.x/1 x, .y empty? y, .c/1 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/0 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/1 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/0 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/1 c1 c, .r |0 r},\n {.x/1 x, .y/0 y, .c/0 c0 c, .r |1 r},\n {.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}\n > _minus_\n>;\n\nplus_bits = {.x inv x, .y inv y, c0 c} _plus inv nat;\nminus_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;\n\n_times = ?{ bits[] = Y y, ... }\n<\n {.y empty? if, .result then} .then,\n <\n {.result |0 result, ._x _x, .y /0 y},\n {{._x x, .result |0 y, c0 c} _plus result, ._x _x, .y /1 y}\n > _times\n>;\ntimes_bits = {0 result, .x inv _x, .y y} _times inv nat;\n\nunat_to_zeros =\n ? unat[] < /_ 0b, /1 unat_to_zeros |0 > ? bits[];\nunat_to_double_zeros =\n ? unat[] < /_ 0b, /1 unat_to_double_zeros |0 |0 > ? bits[];\n\n_peel2 = < </0,/1> </0,/1>, </0,/1>, () >;\n_half_unat = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .y empty? and, .m then} .then,\n {.x _peel2 x, .y _peel2 y, .m |1 m} _half_unat\n>;\nhalf_unat = {.x x, .y y, 0u m} _half_unat;\n\n_split_rev_u = ?{ bits[] = X x_rev, unat[] = M m, ... }\n<\n {.m /_ if, {.low nat low, .x_rev inv nat high} then} .then,\n {.x_rev empty? if, {.low nat low, 0 high} then} .then,\n <\n {.x_rev /0 x_rev, .m /1 m, .low |0 low},\n {.x_rev /1 x_rev, .m /1 m, .low |1 low}\n > _split_rev_u\n>;\nsplit_at = {.x inv x_rev, .m m, 0b low} _split_rev_u;\n\nat_least_256_u? = <\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n yes, no\n>;\n\nshift_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_zeros y} concat\n>;\n\nshift2_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_double_zeros y} concat\n>;\n\n_karatsuba = ?{ bits[] = X x, bits[] = Y y }\n<\n -- Base case: if m < 256 (< 512 bits), fall back to times_bits\n {{.x x, .y y} half_unat m, .x x, .y y}\n <\n {\n .m at_least_256_u? /no if,\n {.x x, .y y} times_bits then\n } .then,\n -- Recursive Karatsuba:\n {\n {.x x, .m m} split_at sx,\n {.y x, .m m} split_at sy,\n .m m\n }\n {\n {.sx.high x, .sy.high y} _karatsuba z2,\n {.sx.low x, .sy.low y} _karatsuba z0,\n {\n {.sx.high x, .sx.low y} plus_bits x,\n {.sy.high x, .sy.low y} plus_bits y\n } _karatsuba z_sum,\n .m m\n }\n {\n .z2 z2,\n .z0 z0,\n {{.z_sum x, .z2 y} minus_bits x, .z0 y} minus_bits z1,\n .m m\n }\n {\n {.z2 val, .m m} shift2_bits_u z2_s,\n {.z1 val, .m m} shift_bits_u z1_s,\n .z0 z0\n }\n {{.z2_s x, .z1_s y} plus_bits x, .z0 y} plus_bits\n >\n>;\n\nkaratsuba =\n{\n <\n {.x/'+' if, .y /'+' and} sign_plus,\n {.x/'-' if, .y /'-' and} sign_plus,\n sign_minus\n > sign,\n {.x abs x, .y abs y} _karatsuba result\n}\n<\n {.sign/'+' if, .result int then} .then,\n {.sign/'-' if, .result int swap_sign then} .then\n>;\n\n()\n",
12
12
  "Examples/karatsuba-mult.k": "-- Karatsuba multiplication O(n^1.585)\n-- Self-contained and minimal implementation\n-- for arbitrary-precision integers\n\nyes = {} | yes;\nno = {} | no;\n\nbits[] = < {} _, B 0, B 1 > = B;\nbits? = ? bits[];\n0b = {} | _;\n0 = 0b |0;\n1 = 0b |1;\n\nunat[] = < {} _, U 1 > = U;\nunat? = ? unat[];\n0u = {} | _;\n\nempty = < /_ yes, no >;\nempty? = {empty /yes if, () then} .then;\n\ninv = {()x, 0b r} _inv;\n_inv = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .r then} .then,\n <\n {.x /0 x, .r |0 r},\n {.x /1 x, .r |1 r}\n > _inv\n>;\n\nconcat = {.x inv x, .y y} concat_;\nconcat_ = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .y then} .then,\n <\n {.x /0 x, .y |0 y},\n {.x /1 x, .y |1 y}\n > concat_\n>;\n\nremove_leading_zeros = < /0 remove_leading_zeros, () >;\nnat = remove_leading_zeros <empty? 0, ()>;\n\nint = nat | '+';\nabs = </'+', /'-'>;\n\nsign_plus = {} | '+';\nsign_minus = {} | '-';\nswap_sign = < /'+' | '-', /'-' | '+' >;\n\nc0 = {} | 0;\nc1 = {} | 1;\n\n_plus = {.x x, .y y, .c c, 0b r} _plus_;\n_plus_ = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,\n {.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,\n {.x empty? if, .c/0 and, {.r x, .y r} _inv then} .then,\n <\n {.x y, .y empty? x, .c c, .r r},\n {<.c/0 0, 1> x, .y y, .x empty? c0 c, .r r},\n {.x /0 x, .y /0 y, .c/0 c0 c, .r |0 r},\n {.x /0 x, .y /0 y, .c/1 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/0 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /0 y, .c/0 c0 c, .r |1 r},\n {.x /1 x, .y /0 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}\n > _plus_\n>;\n\n_minus = {.x x, .y y, .c c, 0b r} _minus_;\n_minus_ = ?{ bits[] = X x, ... }\n<\n {.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,\n <\n {.x/0 x, .y empty? y, .c/1 c1 c, .r |1 r},\n {.x/1 x, .y empty? y, .c/1 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/0 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/1 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/0 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/1 c1 c, .r |0 r},\n {.x/1 x, .y/0 y, .c/0 c0 c, .r |1 r},\n {.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}\n > _minus_\n>;\n\nplus_bits = {.x inv x, .y inv y, c0 c} _plus inv nat;\nminus_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;\n\n_times = ?{ bits[] = Y y, ... }\n<\n {.y empty? if, .result then} .then,\n <\n {.result |0 result, ._x _x, .y /0 y},\n {{._x x, .result |0 y, c0 c} _plus result, ._x _x, .y /1 y}\n > _times\n>;\ntimes_bits = {0 result, .x inv _x, .y y} _times inv nat;\n\nunat_to_zeros =\n ? unat[] < /_ 0b, /1 unat_to_zeros |0 > ? bits[];\nunat_to_double_zeros =\n ? unat[] < /_ 0b, /1 unat_to_double_zeros |0 |0 > ? bits[];\n\n_peel2 = < </0,/1> </0,/1>, </0,/1>, () >;\n_half_unat = ?{ bits[] = X x, ... }\n<\n {.x empty? if, .y empty? and, .m then} .then,\n {.x _peel2 x, .y _peel2 y, .m |1 m} _half_unat\n>;\nhalf_unat = {.x x, .y y, 0u m} _half_unat;\n\n_split_rev_u = ?{ bits[] = X x_rev, unat[] = M m, ... }\n<\n {.m /_ if, {.low nat low, .x_rev inv nat high} then} .then,\n {.x_rev empty? if, {.low nat low, 0 high} then} .then,\n <\n {.x_rev /0 x_rev, .m /1 m, .low |0 low},\n {.x_rev /1 x_rev, .m /1 m, .low |1 low}\n > _split_rev_u\n>;\nsplit_at = {.x inv x_rev, .m m, 0b low} _split_rev_u;\n\nat_least_256_u? = <\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n yes, no\n>;\n\nshift_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_zeros y} concat\n>;\n\nshift2_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_double_zeros y} concat\n>;\n\n_karatsuba = ?{ bits[] = X x, bits[] = Y y }\n<\n -- Base case: if m < 256 (< 512 bits), fall back to times_bits\n {{.x x, .y y} half_unat m, .x x, .y y}\n <\n {\n .m at_least_256_u? /no if,\n {.x x, .y y} times_bits then\n } .then,\n -- Recursive Karatsuba:\n {\n {.x x, .m m} split_at sx,\n {.y x, .m m} split_at sy,\n .m m\n }\n {\n {.sx.high x, .sy.high y} _karatsuba z2,\n {.sx.low x, .sy.low y} _karatsuba z0,\n {\n {.sx.high x, .sx.low y} plus_bits x,\n {.sy.high x, .sy.low y} plus_bits y\n } _karatsuba z_sum,\n .m m\n }\n {\n .z2 z2,\n .z0 z0,\n {{.z_sum x, .z2 y} minus_bits x, .z0 y} minus_bits z1,\n .m m\n }\n {\n {.z2 val, .m m} shift2_bits_u z2_s,\n {.z1 val, .m m} shift_bits_u z1_s,\n .z0 z0\n }\n {{.z2_s x, .z1_s y} plus_bits x, .z0 y} plus_bits\n >\n>;\n\nkaratsuba =\n{\n <\n {.x/'+' if, .y /'+' and} sign_plus,\n {.x/'-' if, .y /'-' and} sign_plus,\n sign_minus\n > sign,\n {.x abs x, .y abs y} _karatsuba result\n}\n<\n {.sign/'+' if, .result int then} .then,\n {.sign/'-' if, .result int swap_sign then} .then\n>;\n\n()\n",
13
- "djson.k": "-- Examples/djson.k: Universal dynamic JSON schema and helper relations\n-- Works with codecs/djson.mjs (k-djson) and codecs/utf8.mjs (k-utf8).\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nlist[X] = < {} nil, { X car, L cdr } cons > = L;\n\npair[K, V] = { K key, V val };\n\nobject[V] = list[pair[string[], V]];\n\ndjson[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n list[J] list,\n object[J] object\n> = J;\n\njson? = ? djson[];\n\nbit? = ? bit[];\nstring? = ? string[];\nfloat64? = ? number[];\nbool? = ? bool[];\nnull? = ? {};\nlist? = ? list[X];\nobject? = ? list[pair[K, V]];\n\n---------------------------------------------------------------\n-- §2. Constructors and Projections\n---------------------------------------------------------------\n\nnil = {} | nil list?;\ncons = {.car car, .cdr cdr} | cons list?;\ncar = list? /cons .car;\ncdr = list? /cons .cdr;\n\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = float64? | number json?;\nstring = string? | string json?;\nbool = bool? | bool json?;\nlist = list? | list json?;\nobject = object? | object json?;\n\n---------------------------------------------------------------\n-- §3. String Equality (eq_string?)\n---------------------------------------------------------------\n\neq_bit? = ?{ <{} 0, {} 1>=B x, B y }\n < {.x /0 x, .y /0 y}, {.x /1 x, .y /1 y} > {};\n\neq_bits1? = { {.x .0 x, .y .0 y} eq_bit? 0 } {};\neq_bits2? = { {.x x, .y y} eq_bits1? a, {.x .1 x, .y .1 y} eq_bit? 1 } {};\neq_bits3? = { {.x x, .y y} eq_bits2? a, {.x .2 x, .y .2 y} eq_bit? 2 } {};\neq_bits4? = { {.x x, .y y} eq_bits3? a, {.x .3 x, .y .3 y} eq_bit? 3 } {};\neq_bits7? = {\n {.x x, .y y} eq_bits4? a,\n {.x .4 x, .y .4 y} eq_bit? 4,\n {.x .5 x, .y .5 y} eq_bit? 5,\n {.x .6 x, .y .6 y} eq_bit? 6\n} {};\neq_bits8? = { {.x x, .y y} eq_bits7? a, {.x .7 x, .y .7 y} eq_bit? 7 } {};\neq_bits11? = {\n {.x x, .y y} eq_bits8? a,\n {.x .8 x, .y .8 y} eq_bit? 8,\n {.x .9 x, .y .9 y} eq_bit? 9,\n {.x .10 x, .y .10 y} eq_bit? 10\n} {};\n\neq_bmp_common_hi? = <\n {.x /h08_0F x, .y /h08_0F y} eq_bits3?,\n {.x /h10_7F x, .y /h10_7F y} eq_bits7?,\n {.x /h80_CF x, .y /h80_CF y} eq_bits7?,\n {.x /hD0_D7 x, .y /hD0_D7 y} eq_bits3?,\n {.x /hF9 x, .y /hF9 y} {},\n {.x /hFA_FB x, .y /hFA_FB y} eq_bits1?,\n {.x /hFC_FD x, .y /hFC_FD y} eq_bits1?,\n {.x /hFE_FF x, .y /hFE_FF y} eq_bits1?\n>;\n\neq_bmp_private_use_hi? = <\n {.x /hE0_EF x, .y /hE0_EF y} eq_bits4?,\n {.x /hF0_F7 x, .y /hF0_F7 y} eq_bits3?,\n {.x /hF8 x, .y /hF8 y} {}\n>;\n\neq_plane2_16? = <\n {.x /p02_03 x, .y /p02_03 y} eq_bits1?,\n {.x /p04_07 x, .y /p04_07 y} eq_bits2?,\n {.x /p08_0F x, .y /p08_0F y} eq_bits3?,\n {.x /p10 x, .y /p10 y} {}\n>;\n\neq_char? = <\n {.x /ascii x, .y /ascii y} eq_bits7?,\n {\n {.x /bmp_common .hi x, .y /bmp_common .hi y} eq_bmp_common_hi? hi,\n {.x /bmp_common .lo x, .y /bmp_common .lo y} eq_bits8? lo\n } {},\n {\n {\n .x /bmp_private_use .hi x,\n .y /bmp_private_use .hi y\n } eq_bmp_private_use_hi? hi,\n {.x /bmp_private_use .lo x, .y /bmp_private_use .lo y} eq_bits8? lo\n } {},\n {.x /plane0 x, .y /plane0 y} eq_bits11?,\n {\n {\n .x /supplementary_plane1 .mid x,\n .y /supplementary_plane1 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_plane1 .lo x,\n .y /supplementary_plane1 .lo y\n } eq_bits8? lo\n } {},\n {\n {\n .x /supplementary_planes2_16 .plane x,\n .y /supplementary_planes2_16 .plane y\n } eq_plane2_16? plane,\n {\n .x /supplementary_planes2_16 .mid x,\n .y /supplementary_planes2_16 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_planes2_16 .lo x,\n .y /supplementary_planes2_16 .lo y\n } eq_bits8? lo\n } {}\n>;\n\n_eq_string? = {.x string? x, .y string? y} <\n {.x /nil x, .y /nil y} {},\n {\n {.x /cons .car x, .y /cons .car y} eq_char? if,\n {.x /cons .cdr x, .y /cons .cdr y} then\n } .then _eq_string?\n>;\n\n-- Identity filter on {x, y} when strings x and y are equal\neq_string? = ?{ S x, S y } {_eq_string? if, () then} .then;\n\n---------------------------------------------------------------\n-- §4. JSON Object Helpers (dot, has_key?, keys, values, remove, put)\n---------------------------------------------------------------\n\n-- Lookup key in object entry list: {key, object} -> val\ndot = ?{ K key, O object } {.key string? key, .object object? object} <\n {\n {.key x, .object /cons .car .key y} eq_string? if,\n .object /cons .car .val then\n } .then,\n {.key key, .object /cons .cdr object} dot\n>;\n\n-- Identity filter on {key, object} when key exists in object\nhas_key? = ?{ K key, O object } {dot if, () then} .then;\n\n-- Extract list of keys from an object entry list: object -> list\nkeys = object? <\n /nil nil,\n /cons {.car .key car, .cdr keys cdr} cons\n>;\n\n-- Extract list of values from an object entry list: object -> list\nvalues = object? <\n /nil nil,\n /cons {.car .val car, .cdr values cdr} cons\n>;\n\n-- Remove all entries matching key from object: {key, object} -> object\nremove = ?{ K key, O object } {.key string? key, .object object? object} <\n {.object /nil if, nil then} .then,\n {\n {.key x, .object /cons .car .key y} eq_string? if,\n {.key key, .object /cons .cdr object} remove then\n } .then,\n {\n .object /cons .car car,\n {.key key, .object /cons .cdr object} remove cdr\n } cons\n>;\n\n-- Insert or replace {key, val} in object: {key, val, object} -> object\nput = ?{ K key, V val, O object } {\n {.key string? key, .val val} car,\n {.key key, .object object} remove cdr\n} cons;\n\n-- Lookup bits? index in cons-list: {index, list} -> value\nbits? = ?< {} _, bits? 0, bits? 1 > = bits?;\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\n\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_dec = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /1 x, .y |0 r } _inv,\n { .x /0 x, .y |1 y } _dec\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\n_trim0 = bits? < /0 _trim0, /_ 0, () >;\ndec_nat = { { () x, 0b r } _inv x, 0b y } _dec _trim0;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\nnth = ?{ I index, L list } { .index bits? index, .list list? list } <\n { .index /0 /_ if, .list /cons .car then } .then,\n { .index dec_nat index, .list /cons .cdr list } nth\n>;\n\n-- Count elements in list or object: list -> bits?, object -> bits?\n_list_size = ?{\n < {} nil, { X car, L cdr } cons >=L list,\n < {} _, B 0, B 1 >=B acc\n} <\n { .list /nil if, .acc then } .then,\n { .list /cons .cdr list, .acc inc_nat acc } _list_size\n> bits?;\nlist_size = { list? list, 0 acc } _list_size;\n\n_obj_size = ?{\n < {} nil, { { K key, V val } car, O cdr } cons >=O object,\n < {} _, B 0, B 1 >=B acc\n} <\n { .object /nil if, .acc then } .then,\n { .object /cons .cdr object, .acc inc_nat acc } _obj_size\n> bits?;\nobj_size = { object? object, 0 acc } _obj_size;\n",
14
- "Examples/djson.k": "-- Examples/djson.k: Universal dynamic JSON schema and helper relations\n-- Works with codecs/djson.mjs (k-djson) and codecs/utf8.mjs (k-utf8).\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nlist[X] = < {} nil, { X car, L cdr } cons > = L;\n\npair[K, V] = { K key, V val };\n\nobject[V] = list[pair[string[], V]];\n\ndjson[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n list[J] list,\n object[J] object\n> = J;\n\njson? = ? djson[];\n\nbit? = ? bit[];\nstring? = ? string[];\nfloat64? = ? number[];\nbool? = ? bool[];\nnull? = ? {};\nlist? = ? list[X];\nobject? = ? list[pair[K, V]];\n\n---------------------------------------------------------------\n-- §2. Constructors and Projections\n---------------------------------------------------------------\n\nnil = {} | nil list?;\ncons = {.car car, .cdr cdr} | cons list?;\ncar = list? /cons .car;\ncdr = list? /cons .cdr;\n\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = float64? | number json?;\nstring = string? | string json?;\nbool = bool? | bool json?;\nlist = list? | list json?;\nobject = object? | object json?;\n\n---------------------------------------------------------------\n-- §3. String Equality (eq_string?)\n---------------------------------------------------------------\n\neq_bit? = ?{ <{} 0, {} 1>=B x, B y }\n < {.x /0 x, .y /0 y}, {.x /1 x, .y /1 y} > {};\n\neq_bits1? = { {.x .0 x, .y .0 y} eq_bit? 0 } {};\neq_bits2? = { {.x x, .y y} eq_bits1? a, {.x .1 x, .y .1 y} eq_bit? 1 } {};\neq_bits3? = { {.x x, .y y} eq_bits2? a, {.x .2 x, .y .2 y} eq_bit? 2 } {};\neq_bits4? = { {.x x, .y y} eq_bits3? a, {.x .3 x, .y .3 y} eq_bit? 3 } {};\neq_bits7? = {\n {.x x, .y y} eq_bits4? a,\n {.x .4 x, .y .4 y} eq_bit? 4,\n {.x .5 x, .y .5 y} eq_bit? 5,\n {.x .6 x, .y .6 y} eq_bit? 6\n} {};\neq_bits8? = { {.x x, .y y} eq_bits7? a, {.x .7 x, .y .7 y} eq_bit? 7 } {};\neq_bits11? = {\n {.x x, .y y} eq_bits8? a,\n {.x .8 x, .y .8 y} eq_bit? 8,\n {.x .9 x, .y .9 y} eq_bit? 9,\n {.x .10 x, .y .10 y} eq_bit? 10\n} {};\n\neq_bmp_common_hi? = <\n {.x /h08_0F x, .y /h08_0F y} eq_bits3?,\n {.x /h10_7F x, .y /h10_7F y} eq_bits7?,\n {.x /h80_CF x, .y /h80_CF y} eq_bits7?,\n {.x /hD0_D7 x, .y /hD0_D7 y} eq_bits3?,\n {.x /hF9 x, .y /hF9 y} {},\n {.x /hFA_FB x, .y /hFA_FB y} eq_bits1?,\n {.x /hFC_FD x, .y /hFC_FD y} eq_bits1?,\n {.x /hFE_FF x, .y /hFE_FF y} eq_bits1?\n>;\n\neq_bmp_private_use_hi? = <\n {.x /hE0_EF x, .y /hE0_EF y} eq_bits4?,\n {.x /hF0_F7 x, .y /hF0_F7 y} eq_bits3?,\n {.x /hF8 x, .y /hF8 y} {}\n>;\n\neq_plane2_16? = <\n {.x /p02_03 x, .y /p02_03 y} eq_bits1?,\n {.x /p04_07 x, .y /p04_07 y} eq_bits2?,\n {.x /p08_0F x, .y /p08_0F y} eq_bits3?,\n {.x /p10 x, .y /p10 y} {}\n>;\n\neq_char? = <\n {.x /ascii x, .y /ascii y} eq_bits7?,\n {\n {.x /bmp_common .hi x, .y /bmp_common .hi y} eq_bmp_common_hi? hi,\n {.x /bmp_common .lo x, .y /bmp_common .lo y} eq_bits8? lo\n } {},\n {\n {\n .x /bmp_private_use .hi x,\n .y /bmp_private_use .hi y\n } eq_bmp_private_use_hi? hi,\n {.x /bmp_private_use .lo x, .y /bmp_private_use .lo y} eq_bits8? lo\n } {},\n {.x /plane0 x, .y /plane0 y} eq_bits11?,\n {\n {\n .x /supplementary_plane1 .mid x,\n .y /supplementary_plane1 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_plane1 .lo x,\n .y /supplementary_plane1 .lo y\n } eq_bits8? lo\n } {},\n {\n {\n .x /supplementary_planes2_16 .plane x,\n .y /supplementary_planes2_16 .plane y\n } eq_plane2_16? plane,\n {\n .x /supplementary_planes2_16 .mid x,\n .y /supplementary_planes2_16 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_planes2_16 .lo x,\n .y /supplementary_planes2_16 .lo y\n } eq_bits8? lo\n } {}\n>;\n\n_eq_string? = {.x string? x, .y string? y} <\n {.x /nil x, .y /nil y} {},\n {\n {.x /cons .car x, .y /cons .car y} eq_char? if,\n {.x /cons .cdr x, .y /cons .cdr y} then\n } .then _eq_string?\n>;\n\n-- Identity filter on {x, y} when strings x and y are equal\neq_string? = ?{ S x, S y } {_eq_string? if, () then} .then;\n\n---------------------------------------------------------------\n-- §4. JSON Object Helpers (dot, has_key?, keys, values, remove, put)\n---------------------------------------------------------------\n\n-- Lookup key in object entry list: {key, object} -> val\ndot = ?{ K key, O object } {.key string? key, .object object? object} <\n {\n {.key x, .object /cons .car .key y} eq_string? if,\n .object /cons .car .val then\n } .then,\n {.key key, .object /cons .cdr object} dot\n>;\n\n-- Identity filter on {key, object} when key exists in object\nhas_key? = ?{ K key, O object } {dot if, () then} .then;\n\n-- Extract list of keys from an object entry list: object -> list\nkeys = object? <\n /nil nil,\n /cons {.car .key car, .cdr keys cdr} cons\n>;\n\n-- Extract list of values from an object entry list: object -> list\nvalues = object? <\n /nil nil,\n /cons {.car .val car, .cdr values cdr} cons\n>;\n\n-- Remove all entries matching key from object: {key, object} -> object\nremove = ?{ K key, O object } {.key string? key, .object object? object} <\n {.object /nil if, nil then} .then,\n {\n {.key x, .object /cons .car .key y} eq_string? if,\n {.key key, .object /cons .cdr object} remove then\n } .then,\n {\n .object /cons .car car,\n {.key key, .object /cons .cdr object} remove cdr\n } cons\n>;\n\n-- Insert or replace {key, val} in object: {key, val, object} -> object\nput = ?{ K key, V val, O object } {\n {.key string? key, .val val} car,\n {.key key, .object object} remove cdr\n} cons;\n\n-- Lookup bits? index in cons-list: {index, list} -> value\nbits? = ?< {} _, bits? 0, bits? 1 > = bits?;\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\n\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_dec = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /1 x, .y |0 r } _inv,\n { .x /0 x, .y |1 y } _dec\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\n_trim0 = bits? < /0 _trim0, /_ 0, () >;\ndec_nat = { { () x, 0b r } _inv x, 0b y } _dec _trim0;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\nnth = ?{ I index, L list } { .index bits? index, .list list? list } <\n { .index /0 /_ if, .list /cons .car then } .then,\n { .index dec_nat index, .list /cons .cdr list } nth\n>;\n\n-- Count elements in list or object: list -> bits?, object -> bits?\n_list_size = ?{\n < {} nil, { X car, L cdr } cons >=L list,\n < {} _, B 0, B 1 >=B acc\n} <\n { .list /nil if, .acc then } .then,\n { .list /cons .cdr list, .acc inc_nat acc } _list_size\n> bits?;\nlist_size = { list? list, 0 acc } _list_size;\n\n_obj_size = ?{\n < {} nil, { { K key, V val } car, O cdr } cons >=O object,\n < {} _, B 0, B 1 >=B acc\n} <\n { .object /nil if, .acc then } .then,\n { .object /cons .cdr object, .acc inc_nat acc } _obj_size\n> bits?;\nobj_size = { object? object, 0 acc } _obj_size;\n",
15
- "djson-btrie.k": "-- Examples/djson-btrie.k: Binary Bit-Trie JSON schema and helpers\n-- Works with codecs/djson-btrie.mjs and codecs/utf8.mjs.\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nopt[V] = < {} nil, V val >;\n\nbtrie[V] = <\n {} nil,\n {\n opt[V] val,\n T 0,\n T 1\n } trie\n> = T;\n\ndjson_btrie[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n btrie[J] list,\n btrie[J] object\n> = J;\n\njson? = ? djson_btrie[];\n\n---------------------------------------------------------------\n-- §2. Constructors and Projections\n---------------------------------------------------------------\n\nbits? = ?< {} _, B 0, B 1 > = B;\nstr_list? = ?< {} nil, { C car, S cdr } cons > = S;\ntrie? = ? btrie[V];\n\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\n\nnil = {} | nil;\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = | number json?;\nstring = | string json?;\nbool = | bool json?;\nlist = | list json?;\nobject = | object json?;\n\n---------------------------------------------------------------\n-- §3. Direct Binary Bit-Trie Traversal (Zero Equality Checks)\n---------------------------------------------------------------\n\n-- Step one bit (0 or 1) in a binary trie\nstep_0 = /trie .0;\nstep_1 = /trie .1;\n\nstep_bit = <\n { .b /0 if, .t step_0 then } .then,\n { .b /1 if, .t step_1 then } .then\n>;\n\n-- Step N bits from MSB (.N-1) down to LSB (.0)\nstep_bits1 = { .x .0 b, .t t } step_bit;\nstep_bits2 = { .x x, { .x .1 b, .t t } step_bit t } step_bits1;\nstep_bits3 = { .x x, { .x .2 b, .t t } step_bit t } step_bits2;\nstep_bits4 = { .x x, { .x .3 b, .t t } step_bit t } step_bits3;\nstep_bits5 = { .x x, { .x .4 b, .t t } step_bit t } step_bits4;\nstep_bits6 = { .x x, { .x .5 b, .t t } step_bit t } step_bits5;\nstep_bits7 = { .x x, { .x .6 b, .t t } step_bit t } step_bits6;\nstep_bits8 = { .x x, { .x .7 b, .t t } step_bit t } step_bits7;\nstep_bits9 = { .x x, { .x .8 b, .t t } step_bit t } step_bits8;\nstep_bits10 = { .x x, { .x .9 b, .t t } step_bit t } step_bits9;\nstep_bits11 = { .x x, { .x .10 b, .t t } step_bit t } step_bits10;\n\nstep_bmp_common_hi = <\n { .hi /h08_0F x, .t step_0 step_0 step_0 t } step_bits3,\n { .hi /h10_7F x, .t step_0 step_0 step_1 t } step_bits7,\n { .hi /h80_CF x, .t step_0 step_1 step_0 t } step_bits7,\n { .hi /hD0_D7 x, .t step_0 step_1 step_1 t } step_bits3,\n { .hi /hF9 if, .t step_1 step_0 step_0 then } .then,\n { .hi /hFA_FB x, .t step_1 step_0 step_1 t } step_bits1,\n { .hi /hFC_FD x, .t step_1 step_1 step_0 t } step_bits1,\n { .hi /hFE_FF x, .t step_1 step_1 step_1 t } step_bits1\n>;\n\nstep_bmp_priv_hi = <\n { .hi /hE0_EF x, .t step_0 t } step_bits4,\n { .hi /hF0_F7 x, .t step_1 step_0 t } step_bits3,\n { .hi /hF8 if, .t step_1 step_1 then } .then\n>;\n\nstep_plane2_16 = <\n { .p /p02_03 x, .t step_0 step_0 t } step_bits1,\n { .p /p04_07 x, .t step_0 step_1 t } step_bits2,\n { .p /p08_0F x, .t step_1 step_0 t } step_bits3,\n { .p /p10 if, .t step_1 step_1 then } .then\n>;\n\n-- Advance trie by one Unicode character: { char, t } -> t\nstep_char = <\n { .char /ascii x, .t step_0 t } step_bits7,\n { .char /plane0 x, .t step_1 step_0 t } step_bits11,\n {\n .char /bmp_common .lo x,\n { .char /bmp_common .hi hi, .t step_1 step_1 step_0 t }\n step_bmp_common_hi t\n } step_bits8,\n {\n .char /bmp_private_use .lo x,\n { .char /bmp_private_use .hi hi, .t step_1 step_1 step_1 step_0 t }\n step_bmp_priv_hi t\n } step_bits8,\n {\n .char /supplementary_plane1 .lo x,\n {\n .char /supplementary_plane1 .mid x,\n .t step_1 step_1 step_1 step_1 step_0 t\n } step_bits8 t\n } step_bits8,\n {\n .char /supplementary_planes2_16 .lo x,\n {\n .char /supplementary_planes2_16 .mid x,\n {\n .char /supplementary_planes2_16 .plane p,\n .t step_1 step_1 step_1 step_1 step_1 t\n } step_plane2_16 t\n } step_bits8 t\n } step_bits8\n>;\n\n---------------------------------------------------------------\n-- §4. Object & List Lookup Relations (dot, nth, has_key?)\n---------------------------------------------------------------\n\n-- Lookup string key in object trie: { key, object } -> value\ndot = ?{ K key, T object } { .key str_list? key, .object trie? t } <\n { .key /nil if, .t /trie .val /val then } .then,\n {\n .key /cons .cdr key,\n { .key /cons .car char, .t t } step_char object\n } dot\n>;\n\n-- Filter: succeeds as identity if object contains key\nhas_key? = ?{ K key, O object } { dot if, () then } .then;\n\n-- Lookup bits? index in list trie: { index, list } -> value\nnth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .index /_ if, .t /trie .val /val then } .then,\n { .index /0 index, .t /trie .0 list } nth,\n { .index /1 index, .t /trie .1 list } nth\n>;\n\n---------------------------------------------------------------\n-- §5. Trie Update & Removal (put, remove, put_nth, remove_nth)\n---------------------------------------------------------------\n\npush_bit = ?{ < {} 0, {} 1 > b, < {} _, BITS 0, BITS 1 >=BITS acc } <\n { .b /0 if, .acc |0 then } .then,\n { .b /1 if, .acc |1 then } .then\n> bits?;\n\npush_bits1 = { .x .0 b, .acc acc } push_bit;\npush_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;\npush_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;\npush_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;\npush_bits5 = { .x .4 b, { .x x, .acc acc } push_bits4 acc } push_bit;\npush_bits6 = { .x .5 b, { .x x, .acc acc } push_bits5 acc } push_bit;\npush_bits7 = { .x .6 b, { .x x, .acc acc } push_bits6 acc } push_bit;\npush_bits8 = { .x .7 b, { .x x, .acc acc } push_bits7 acc } push_bit;\npush_bits9 = { .x .8 b, { .x x, .acc acc } push_bits8 acc } push_bit;\npush_bits10 = { .x .9 b, { .x x, .acc acc } push_bits9 acc } push_bit;\npush_bits11 = { .x .10 b, { .x x, .acc acc } push_bits10 acc } push_bit;\n\npush_bmp_common_hi = <\n { .hi /h08_0F x, .acc acc } push_bits3 |0 |0 |0,\n { .hi /h10_7F x, .acc acc } push_bits7 |1 |0 |0,\n { .hi /h80_CF x, .acc acc } push_bits7 |0 |1 |0,\n { .hi /hD0_D7 x, .acc acc } push_bits3 |1 |1 |0,\n { .hi /hF9 if, .acc |0 |0 |1 then } .then,\n { .hi /hFA_FB x, .acc acc } push_bits1 |1 |0 |1,\n { .hi /hFC_FD x, .acc acc } push_bits1 |0 |1 |1,\n { .hi /hFE_FF x, .acc acc } push_bits1 |1 |1 |1\n> bits?;\n\npush_bmp_priv_hi = <\n { .hi /hE0_EF x, .acc acc } push_bits4 |0,\n { .hi /hF0_F7 x, .acc acc } push_bits3 |0 |1,\n { .hi /hF8 if, .acc |1 |1 then } .then\n> bits?;\n\npush_plane2_16 = <\n { .p /p02_03 x, .acc acc } push_bits1 |0 |0,\n { .p /p04_07 x, .acc acc } push_bits2 |1 |0,\n { .p /p08_0F x, .acc acc } push_bits3 |0 |1,\n { .p /p10 if, .acc |1 |1 then } .then\n> bits?;\n\nprepend_char_bits = <\n { .char /ascii x, .acc acc } push_bits7 |0,\n { .char /plane0 x, .acc acc } push_bits11 |0 |1,\n {\n .char /bmp_common .hi hi,\n { .char /bmp_common .lo x, .acc acc } push_bits8 acc\n } push_bmp_common_hi |0 |1 |1,\n {\n .char /bmp_private_use .hi hi,\n { .char /bmp_private_use .lo x, .acc acc } push_bits8 acc\n } push_bmp_priv_hi |0 |1 |1 |1,\n {\n .char /supplementary_plane1 .mid x,\n { .char /supplementary_plane1 .lo x, .acc acc } push_bits8 acc\n } push_bits8 |0 |1 |1 |1 |1,\n {\n .char /supplementary_planes2_16 .plane p,\n {\n .char /supplementary_planes2_16 .mid x,\n { .char /supplementary_planes2_16 .lo x, .acc acc } push_bits8 acc\n } push_bits8 acc\n } push_plane2_16 |1 |1 |1 |1 |1\n> bits?;\n\nstr_to_bits = str_list? <\n /nil 0b,\n /cons { .car char, .cdr str_to_bits acc } prepend_char_bits\n> bits?;\n\nensure_trie = trie? < /nil { nil val, nil 0, nil 1 }, /trie () >;\n\n-- Insert or replace { index, val, list } -> list\nput_nth = ?{ K index, V val, T list } {\n .index bits? index,\n .val val,\n .list trie? ensure_trie t\n} <\n {\n .index /_ if,\n { .val |val val, .t .0 0, .t .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t .val val,\n { .index /0 index, .val val, .t .0 list } put_nth 0,\n .t .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t .val val,\n .t .0 0,\n { .index /1 index, .val val, .t .1 list } put_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Remove entry at bits? index from trie: { index, list } -> list\nremove_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .t /nil if, nil then } .then,\n {\n .index /_ if,\n { nil val, .t /trie .0 0, .t /trie .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t /trie .val val,\n { .index /0 index, .t /trie .0 list } remove_nth 0,\n .t /trie .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t /trie .val val,\n .t /trie .0 0,\n { .index /1 index, .t /trie .1 list } remove_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Insert or replace { key, val, object } -> object\nput = ?{ K key, V val, O object } {\n .key str_to_bits index,\n .val val,\n .object list\n} put_nth;\n\n-- Remove key from object trie: { key, object } -> object\nremove = ?{ K key, O object } {\n .key str_to_bits index,\n .object list\n} remove_nth;\n\n-- Count elements in list or object trie: trie -> bits?\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\n_trie_size = ?{\n < {} nil, { < {} nil, V val > val, T 0, T 1 } trie >=T t,\n < {} _, B 0, B 1 >=B acc\n} <\n { .t /nil if, .acc then } .then,\n {\n .t /trie .val /nil if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc acc } _trie_size acc\n } _trie_size then\n } .then,\n {\n .t /trie .val /val if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc inc_nat acc } _trie_size acc\n } _trie_size then\n } .then\n> bits?;\n\nlist_size = { trie? t, 0 acc } _trie_size;\nobj_size = { trie? t, 0 acc } _trie_size;\n",
16
- "Examples/djson-btrie.k": "-- Examples/djson-btrie.k: Binary Bit-Trie JSON schema and helpers\n-- Works with codecs/djson-btrie.mjs and codecs/utf8.mjs.\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nopt[V] = < {} nil, V val >;\n\nbtrie[V] = <\n {} nil,\n {\n opt[V] val,\n T 0,\n T 1\n } trie\n> = T;\n\ndjson_btrie[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n btrie[J] list,\n btrie[J] object\n> = J;\n\njson? = ? djson_btrie[];\n\n---------------------------------------------------------------\n-- §2. Constructors and Projections\n---------------------------------------------------------------\n\nbits? = ?< {} _, B 0, B 1 > = B;\nstr_list? = ?< {} nil, { C car, S cdr } cons > = S;\ntrie? = ? btrie[V];\n\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\n\nnil = {} | nil;\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = | number json?;\nstring = | string json?;\nbool = | bool json?;\nlist = | list json?;\nobject = | object json?;\n\n---------------------------------------------------------------\n-- §3. Direct Binary Bit-Trie Traversal (Zero Equality Checks)\n---------------------------------------------------------------\n\n-- Step one bit (0 or 1) in a binary trie\nstep_0 = /trie .0;\nstep_1 = /trie .1;\n\nstep_bit = <\n { .b /0 if, .t step_0 then } .then,\n { .b /1 if, .t step_1 then } .then\n>;\n\n-- Step N bits from MSB (.N-1) down to LSB (.0)\nstep_bits1 = { .x .0 b, .t t } step_bit;\nstep_bits2 = { .x x, { .x .1 b, .t t } step_bit t } step_bits1;\nstep_bits3 = { .x x, { .x .2 b, .t t } step_bit t } step_bits2;\nstep_bits4 = { .x x, { .x .3 b, .t t } step_bit t } step_bits3;\nstep_bits5 = { .x x, { .x .4 b, .t t } step_bit t } step_bits4;\nstep_bits6 = { .x x, { .x .5 b, .t t } step_bit t } step_bits5;\nstep_bits7 = { .x x, { .x .6 b, .t t } step_bit t } step_bits6;\nstep_bits8 = { .x x, { .x .7 b, .t t } step_bit t } step_bits7;\nstep_bits9 = { .x x, { .x .8 b, .t t } step_bit t } step_bits8;\nstep_bits10 = { .x x, { .x .9 b, .t t } step_bit t } step_bits9;\nstep_bits11 = { .x x, { .x .10 b, .t t } step_bit t } step_bits10;\n\nstep_bmp_common_hi = <\n { .hi /h08_0F x, .t step_0 step_0 step_0 t } step_bits3,\n { .hi /h10_7F x, .t step_0 step_0 step_1 t } step_bits7,\n { .hi /h80_CF x, .t step_0 step_1 step_0 t } step_bits7,\n { .hi /hD0_D7 x, .t step_0 step_1 step_1 t } step_bits3,\n { .hi /hF9 if, .t step_1 step_0 step_0 then } .then,\n { .hi /hFA_FB x, .t step_1 step_0 step_1 t } step_bits1,\n { .hi /hFC_FD x, .t step_1 step_1 step_0 t } step_bits1,\n { .hi /hFE_FF x, .t step_1 step_1 step_1 t } step_bits1\n>;\n\nstep_bmp_priv_hi = <\n { .hi /hE0_EF x, .t step_0 t } step_bits4,\n { .hi /hF0_F7 x, .t step_1 step_0 t } step_bits3,\n { .hi /hF8 if, .t step_1 step_1 then } .then\n>;\n\nstep_plane2_16 = <\n { .p /p02_03 x, .t step_0 step_0 t } step_bits1,\n { .p /p04_07 x, .t step_0 step_1 t } step_bits2,\n { .p /p08_0F x, .t step_1 step_0 t } step_bits3,\n { .p /p10 if, .t step_1 step_1 then } .then\n>;\n\n-- Advance trie by one Unicode character: { char, t } -> t\nstep_char = <\n { .char /ascii x, .t step_0 t } step_bits7,\n { .char /plane0 x, .t step_1 step_0 t } step_bits11,\n {\n .char /bmp_common .lo x,\n { .char /bmp_common .hi hi, .t step_1 step_1 step_0 t }\n step_bmp_common_hi t\n } step_bits8,\n {\n .char /bmp_private_use .lo x,\n { .char /bmp_private_use .hi hi, .t step_1 step_1 step_1 step_0 t }\n step_bmp_priv_hi t\n } step_bits8,\n {\n .char /supplementary_plane1 .lo x,\n {\n .char /supplementary_plane1 .mid x,\n .t step_1 step_1 step_1 step_1 step_0 t\n } step_bits8 t\n } step_bits8,\n {\n .char /supplementary_planes2_16 .lo x,\n {\n .char /supplementary_planes2_16 .mid x,\n {\n .char /supplementary_planes2_16 .plane p,\n .t step_1 step_1 step_1 step_1 step_1 t\n } step_plane2_16 t\n } step_bits8 t\n } step_bits8\n>;\n\n---------------------------------------------------------------\n-- §4. Object & List Lookup Relations (dot, nth, has_key?)\n---------------------------------------------------------------\n\n-- Lookup string key in object trie: { key, object } -> value\ndot = ?{ K key, T object } { .key str_list? key, .object trie? t } <\n { .key /nil if, .t /trie .val /val then } .then,\n {\n .key /cons .cdr key,\n { .key /cons .car char, .t t } step_char object\n } dot\n>;\n\n-- Filter: succeeds as identity if object contains key\nhas_key? = ?{ K key, O object } { dot if, () then } .then;\n\n-- Lookup bits? index in list trie: { index, list } -> value\nnth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .index /_ if, .t /trie .val /val then } .then,\n { .index /0 index, .t /trie .0 list } nth,\n { .index /1 index, .t /trie .1 list } nth\n>;\n\n---------------------------------------------------------------\n-- §5. Trie Update & Removal (put, remove, put_nth, remove_nth)\n---------------------------------------------------------------\n\npush_bit = ?{ < {} 0, {} 1 > b, < {} _, BITS 0, BITS 1 >=BITS acc } <\n { .b /0 if, .acc |0 then } .then,\n { .b /1 if, .acc |1 then } .then\n> bits?;\n\npush_bits1 = { .x .0 b, .acc acc } push_bit;\npush_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;\npush_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;\npush_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;\npush_bits5 = { .x .4 b, { .x x, .acc acc } push_bits4 acc } push_bit;\npush_bits6 = { .x .5 b, { .x x, .acc acc } push_bits5 acc } push_bit;\npush_bits7 = { .x .6 b, { .x x, .acc acc } push_bits6 acc } push_bit;\npush_bits8 = { .x .7 b, { .x x, .acc acc } push_bits7 acc } push_bit;\npush_bits9 = { .x .8 b, { .x x, .acc acc } push_bits8 acc } push_bit;\npush_bits10 = { .x .9 b, { .x x, .acc acc } push_bits9 acc } push_bit;\npush_bits11 = { .x .10 b, { .x x, .acc acc } push_bits10 acc } push_bit;\n\npush_bmp_common_hi = <\n { .hi /h08_0F x, .acc acc } push_bits3 |0 |0 |0,\n { .hi /h10_7F x, .acc acc } push_bits7 |1 |0 |0,\n { .hi /h80_CF x, .acc acc } push_bits7 |0 |1 |0,\n { .hi /hD0_D7 x, .acc acc } push_bits3 |1 |1 |0,\n { .hi /hF9 if, .acc |0 |0 |1 then } .then,\n { .hi /hFA_FB x, .acc acc } push_bits1 |1 |0 |1,\n { .hi /hFC_FD x, .acc acc } push_bits1 |0 |1 |1,\n { .hi /hFE_FF x, .acc acc } push_bits1 |1 |1 |1\n> bits?;\n\npush_bmp_priv_hi = <\n { .hi /hE0_EF x, .acc acc } push_bits4 |0,\n { .hi /hF0_F7 x, .acc acc } push_bits3 |0 |1,\n { .hi /hF8 if, .acc |1 |1 then } .then\n> bits?;\n\npush_plane2_16 = <\n { .p /p02_03 x, .acc acc } push_bits1 |0 |0,\n { .p /p04_07 x, .acc acc } push_bits2 |1 |0,\n { .p /p08_0F x, .acc acc } push_bits3 |0 |1,\n { .p /p10 if, .acc |1 |1 then } .then\n> bits?;\n\nprepend_char_bits = <\n { .char /ascii x, .acc acc } push_bits7 |0,\n { .char /plane0 x, .acc acc } push_bits11 |0 |1,\n {\n .char /bmp_common .hi hi,\n { .char /bmp_common .lo x, .acc acc } push_bits8 acc\n } push_bmp_common_hi |0 |1 |1,\n {\n .char /bmp_private_use .hi hi,\n { .char /bmp_private_use .lo x, .acc acc } push_bits8 acc\n } push_bmp_priv_hi |0 |1 |1 |1,\n {\n .char /supplementary_plane1 .mid x,\n { .char /supplementary_plane1 .lo x, .acc acc } push_bits8 acc\n } push_bits8 |0 |1 |1 |1 |1,\n {\n .char /supplementary_planes2_16 .plane p,\n {\n .char /supplementary_planes2_16 .mid x,\n { .char /supplementary_planes2_16 .lo x, .acc acc } push_bits8 acc\n } push_bits8 acc\n } push_plane2_16 |1 |1 |1 |1 |1\n> bits?;\n\nstr_to_bits = str_list? <\n /nil 0b,\n /cons { .car char, .cdr str_to_bits acc } prepend_char_bits\n> bits?;\n\nensure_trie = trie? < /nil { nil val, nil 0, nil 1 }, /trie () >;\n\n-- Insert or replace { index, val, list } -> list\nput_nth = ?{ K index, V val, T list } {\n .index bits? index,\n .val val,\n .list trie? ensure_trie t\n} <\n {\n .index /_ if,\n { .val |val val, .t .0 0, .t .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t .val val,\n { .index /0 index, .val val, .t .0 list } put_nth 0,\n .t .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t .val val,\n .t .0 0,\n { .index /1 index, .val val, .t .1 list } put_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Remove entry at bits? index from trie: { index, list } -> list\nremove_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .t /nil if, nil then } .then,\n {\n .index /_ if,\n { nil val, .t /trie .0 0, .t /trie .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t /trie .val val,\n { .index /0 index, .t /trie .0 list } remove_nth 0,\n .t /trie .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t /trie .val val,\n .t /trie .0 0,\n { .index /1 index, .t /trie .1 list } remove_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Insert or replace { key, val, object } -> object\nput = ?{ K key, V val, O object } {\n .key str_to_bits index,\n .val val,\n .object list\n} put_nth;\n\n-- Remove key from object trie: { key, object } -> object\nremove = ?{ K key, O object } {\n .key str_to_bits index,\n .object list\n} remove_nth;\n\n-- Count elements in list or object trie: trie -> bits?\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\n_trie_size = ?{\n < {} nil, { < {} nil, V val > val, T 0, T 1 } trie >=T t,\n < {} _, B 0, B 1 >=B acc\n} <\n { .t /nil if, .acc then } .then,\n {\n .t /trie .val /nil if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc acc } _trie_size acc\n } _trie_size then\n } .then,\n {\n .t /trie .val /val if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc inc_nat acc } _trie_size acc\n } _trie_size then\n } .then\n> bits?;\n\nlist_size = { trie? t, 0 acc } _trie_size;\nobj_size = { trie? t, 0 acc } _trie_size;\n",
17
- "djson-ptrie.k": "-- Examples/djson-ptrie.k: Patricia/Radix Trie JSON schema & helpers\n-- Works with codecs/djson-ptrie.mjs (trie-array style).\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nbits[] = < {} _, B 0, B 1 > = B;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nopt[V] = < {} nil, V val >;\n\nptrie_list[V] = <\n {} nil,\n {\n bits[] prefix,\n bits[] size,\n opt[V] val,\n L 0,\n L 1\n } trie\n> = L;\n\npair[A, B] = { A char, B trie };\nlist[X] = < {} nil, { X car, D cdr } cons > = D;\n\nptrie_obj[V] = <\n {} nil,\n {\n string[] prefix,\n bits[] size,\n opt[V] val,\n list[pair[unicode[], O]] dict\n } trie\n> = O;\n\ndjson_ptrie[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n ptrie_list[J] list,\n ptrie_obj[J] object\n> = J;\n\njson? = ? djson_ptrie[];\n\n-----------------------------------------------------------------\n-- §2. Unicode Character Equality (eq_char?)\n---------------------------------------------------------------\n\neq_bit? = ?{ <{} 0, {} 1>=B x, B y }\n < {.x /0 x, .y /0 y}, {.x /1 x, .y /1 y} > {};\n\neq_bits1? = { {.x .0 x, .y .0 y} eq_bit? 0 } {};\neq_bits2? = { {.x x, .y y} eq_bits1? a, {.x .1 x, .y .1 y} eq_bit? 1 } {};\neq_bits3? = { {.x x, .y y} eq_bits2? a, {.x .2 x, .y .2 y} eq_bit? 2 } {};\neq_bits4? = { {.x x, .y y} eq_bits3? a, {.x .3 x, .y .3 y} eq_bit? 3 } {};\neq_bits7? = {\n {.x x, .y y} eq_bits4? a,\n {.x .4 x, .y .4 y} eq_bit? 4,\n {.x .5 x, .y .5 y} eq_bit? 5,\n {.x .6 x, .y .6 y} eq_bit? 6\n} {};\neq_bits8? = { {.x x, .y y} eq_bits7? a, {.x .7 x, .y .7 y} eq_bit? 7 } {};\neq_bits11? = {\n {.x x, .y y} eq_bits8? a,\n {.x .8 x, .y .8 y} eq_bit? 8,\n {.x .9 x, .y .9 y} eq_bit? 9,\n {.x .10 x, .y .10 y} eq_bit? 10\n} {};\n\neq_bmp_common_hi? = <\n {.x /h08_0F x, .y /h08_0F y} eq_bits3?,\n {.x /h10_7F x, .y /h10_7F y} eq_bits7?,\n {.x /h80_CF x, .y /h80_CF y} eq_bits7?,\n {.x /hD0_D7 x, .y /hD0_D7 y} eq_bits3?,\n {.x /hF9 x, .y /hF9 y} {},\n {.x /hFA_FB x, .y /hFA_FB y} eq_bits1?,\n {.x /hFC_FD x, .y /hFC_FD y} eq_bits1?,\n {.x /hFE_FF x, .y /hFE_FF y} eq_bits1?\n>;\n\neq_bmp_private_use_hi? = <\n {.x /hE0_EF x, .y /hE0_EF y} eq_bits4?,\n {.x /hF0_F7 x, .y /hF0_F7 y} eq_bits3?,\n {.x /hF8 x, .y /hF8 y} {}\n>;\n\neq_plane2_16? = <\n {.x /p02_03 x, .y /p02_03 y} eq_bits1?,\n {.x /p04_07 x, .y /p04_07 y} eq_bits2?,\n {.x /p08_0F x, .y /p08_0F y} eq_bits3?,\n {.x /p10 x, .y /p10 y} {}\n>;\n\neq_char? = <\n {.x /ascii x, .y /ascii y} eq_bits7?,\n {\n {.x /bmp_common .hi x, .y /bmp_common .hi y} eq_bmp_common_hi? hi,\n {.x /bmp_common .lo x, .y /bmp_common .lo y} eq_bits8? lo\n } {},\n {\n {\n .x /bmp_private_use .hi x,\n .y /bmp_private_use .hi y\n } eq_bmp_private_use_hi? hi,\n {.x /bmp_private_use .lo x, .y /bmp_private_use .lo y} eq_bits8? lo\n } {},\n {.x /plane0 x, .y /plane0 y} eq_bits11?,\n {\n {\n .x /supplementary_plane1 .mid x,\n .y /supplementary_plane1 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_plane1 .lo x,\n .y /supplementary_plane1 .lo y\n } eq_bits8? lo\n } {},\n {\n {\n .x /supplementary_planes2_16 .plane x,\n .y /supplementary_planes2_16 .plane y\n } eq_plane2_16? plane,\n {\n .x /supplementary_planes2_16 .mid x,\n .y /supplementary_planes2_16 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_planes2_16 .lo x,\n .y /supplementary_planes2_16 .lo y\n } eq_bits8? lo\n } {}\n>;\n\n---------------------------------------------------------------\n-- §3. Patricia/Radix Trie Lookup Relations (dot, nth, has_key?)\n---------------------------------------------------------------\n\nbits? = ?< {} _, bits? 0, bits? 1 > = bits?;\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\nstr_list? = ?< {} nil, { C car, S cdr } cons > = S;\nptrie_list? = ?<\n {} nil,\n { B1 prefix, B2 size, < {} nil, V val > val, T 0, T 1 } trie\n> = T;\nptrie_obj? = ?<\n {} nil,\n {\n S prefix,\n B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n> = T;\nptrie_dict? = ?<\n {} nil,\n { { C char, T trie } car, D cdr } cons\n> = D;\n\nnil = {} | nil;\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = | number json?;\nstring = | string json?;\nbool = | bool json?;\nlist = | list json?;\nobject = | object json?;\n\n-- O(1) subtree size queries (number of elements in list or object)\nlist_size = ptrie_list? < /nil 0, /trie .size >;\nobj_size = ptrie_obj? < /nil 0, /trie .size >;\n\nmatch_str_prefix = ?{ P prefix, K key } {\n .prefix str_list? prefix,\n .key str_list? key\n} <\n { .prefix /nil if, .key then } .then,\n {\n { .prefix /cons .car x, .key /cons .car y } eq_char? if,\n { .prefix /cons .cdr prefix, .key /cons .cdr key }\n match_str_prefix then\n } .then\n>;\n\nfind_in_dict = ?{ C char, D dict } {\n .char char,\n .dict ptrie_dict? dict\n} <\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n .dict /cons .car .trie then\n } .then,\n { .char char, .dict /cons .cdr dict } find_in_dict\n>;\n\n-- Lookup string key in radix object trie: { key, object } -> val\ndot = ?{ K key, T object } {\n { .object ptrie_obj? /trie .prefix prefix, .key str_list? key }\n match_str_prefix rem,\n .object ptrie_obj? /trie t\n} <\n { .rem /nil if, .t .val /val then } .then,\n {\n .rem /cons .cdr key,\n { .rem /cons .car char, .t .dict dict } find_in_dict object\n } dot\n>;\n\nhas_key? = ?{ K key, O object } { dot if, () then } .then;\n\n-- Lookup bits? index in Patricia bit-trie list: { index, list } -> val\nmatch_bit_prefix = ?{ P prefix, K key } {\n .prefix bits? prefix,\n .key bits? key\n} <\n { .prefix /_ if, .key then } .then,\n { .prefix /0 prefix, .key /0 key } match_bit_prefix,\n { .prefix /1 prefix, .key /1 key } match_bit_prefix\n>;\n\nnth = ?{ K index, T list } {\n { .list ptrie_list? /trie .prefix prefix, .index bits? key }\n match_bit_prefix rem,\n .list ptrie_list? /trie t\n} <\n { .rem /_ if, .t .val /val then } .then,\n { .rem /0 index, .t .0 list } nth,\n { .rem /1 index, .t .1 list } nth\n>;\n\n-- Binary natural number helpers for size maintenance\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_dec = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /1 x, .y |0 r } _inv,\n { .x /0 x, .y |1 y } _dec\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\n_trim0 = bits? < /0 _trim0, /_ 0, () >;\ndec_nat = { { () x, 0b r } _inv x, 0b y } _dec _trim0;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\n-- Remove key from radix object trie: { key, object } -> object\n_remove_step = ?<\n {\n < {} nil, { C car, S cdr } cons >=S key,\n <\n {} nil,\n {\n S prefix,\n < {} _, B 0, B 1 >=B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n >=T object\n } node,\n { C char, S key, D dict } dict\n> <\n /node {\n { .object /trie .prefix prefix, .key key } match_str_prefix rem,\n .object /trie t\n } <\n {\n .rem /nil if,\n {\n .t .prefix prefix,\n .t .size dec_nat size,\n nil val,\n .t .dict dict\n } |trie then\n } .then,\n {\n .t .prefix prefix,\n .t .size dec_nat size,\n .t .val val,\n {\n .rem /cons .car char,\n .rem /cons .cdr key,\n .t .dict dict\n } |dict _remove_step /dict dict\n } |trie\n > |node,\n /dict <\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n {\n {\n .char char,\n {\n .key key,\n .dict /cons .car .trie object\n } |node _remove_step /node trie\n } car,\n .dict /cons .cdr cdr\n } |cons then\n } .then,\n {\n .dict /cons .car car,\n {\n .char char,\n .key key,\n .dict /cons .cdr dict\n } |dict _remove_step /dict cdr\n } |cons\n > |dict\n>;\n\nremove = ?{ K key, O object } {\n .key str_list? key,\n .object ptrie_obj? object\n} <\n {\n { .key key, .object object } has_key? if,\n { .key key, .object object } |node _remove_step /node then\n } .then,\n .object\n> ptrie_obj?;\n\n-- Split two strings into { common, rem_prefix, rem_key }\nsplit_str_prefix = ?{\n < {} nil, { C car, S cdr } cons >=S prefix,\n S key\n} <\n {\n .prefix /nil if,\n { nil common, nil rem_prefix, .key rem_key } then\n } .then,\n {\n .key /nil if,\n { nil common, .prefix rem_prefix, nil rem_key } then\n } .then,\n {\n { .prefix /cons .car x, .key /cons .car y } eq_char? if,\n {\n .prefix /cons .car c,\n {\n .prefix /cons .cdr prefix,\n .key /cons .cdr key\n } split_str_prefix sub\n } {\n { .c car, .sub .common cdr } |cons common,\n .sub .rem_prefix rem_prefix,\n .sub .rem_key rem_key\n } then\n } .then,\n { nil common, .prefix rem_prefix, .key rem_key }\n> ?{ S1 common, S2 rem_prefix, S3 rem_key } {\n .common str_list? common,\n .rem_prefix str_list? rem_prefix,\n .rem_key str_list? rem_key\n};\n\n-- Replace value of an existing key in radix object trie\n_put_replace = ?<\n {\n < {} nil, { C car, S cdr } cons >=S key,\n V val,\n <\n {} nil,\n {\n S prefix,\n < {} _, B 0, B 1 >=B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n >=T object\n } node,\n { C char, S key, V val, D dict } dict\n> <\n /node {\n { .object /trie .prefix prefix, .key key } match_str_prefix rem,\n .val val,\n .object /trie t\n } <\n {\n .rem /nil if,\n {\n .t .prefix prefix,\n .t .size size,\n .val |val val,\n .t .dict dict\n } |trie then\n } .then,\n {\n .t .prefix prefix,\n .t .size size,\n .t .val val,\n {\n .rem /cons .car char,\n .rem /cons .cdr key,\n .val val,\n .t .dict dict\n } |dict _put_replace /dict dict\n } |trie\n > |node,\n /dict <\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n {\n {\n .char char,\n {\n .key key,\n .val val,\n .dict /cons .car .trie object\n } |node _put_replace /node trie\n } car,\n .dict /cons .cdr cdr\n } |cons then\n } .then,\n {\n .dict /cons .car car,\n {\n .char char,\n .key key,\n .val val,\n .dict /cons .cdr dict\n } |dict _put_replace /dict cdr\n } |cons\n > |dict\n>;\n\n-- Insert a new key into radix object trie (splitting prefixes as needed)\n_put_insert = ?<\n {\n < {} nil, { C car, S cdr } cons >=S key,\n V val,\n <\n {} nil,\n {\n S prefix,\n < {} _, B 0, B 1 >=B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n >=T object\n } node,\n { C char, S key, V val, D dict } dict\n> <\n /node <\n {\n .object /nil if,\n { .key prefix, 1 size, .val |val val, nil dict } |trie then\n } .then,\n {\n { .object /trie .prefix prefix, .key key } split_str_prefix sp,\n .val val,\n .object /trie t\n } <\n {\n { .sp .rem_prefix /nil p, .sp .rem_key /nil k } if,\n {\n .t .prefix prefix,\n .t .size inc_nat size,\n .val |val val,\n .t .dict dict\n } |trie then\n } .then,\n {\n { .sp .rem_prefix /nil p, .sp .rem_key /cons k } if,\n {\n .t .prefix prefix,\n .t .size inc_nat size,\n .t .val val,\n {\n .sp .rem_key /cons .car char,\n .sp .rem_key /cons .cdr key,\n .val val,\n .t .dict dict\n } |dict _put_insert /dict dict\n } |trie then\n } .then,\n {\n { .sp .rem_prefix /cons p, .sp .rem_key /nil k } if,\n {\n .sp .common prefix,\n .t .size inc_nat size,\n .val |val val,\n {\n {\n .sp .rem_prefix /cons .car char,\n {\n .sp .rem_prefix /cons .cdr prefix,\n .t .size size,\n .t .val val,\n .t .dict dict\n } |trie trie\n } car,\n nil cdr\n } |cons dict\n } |trie then\n } .then,\n {\n .sp .common prefix,\n .t .size inc_nat size,\n nil val,\n {\n {\n .sp .rem_prefix /cons .car char,\n {\n .sp .rem_prefix /cons .cdr prefix,\n .t .size size,\n .t .val val,\n .t .dict dict\n } |trie trie\n } car,\n {\n {\n .sp .rem_key /cons .car char,\n {\n .sp .rem_key /cons .cdr prefix,\n 1 size,\n .val |val val,\n nil dict\n } |trie trie\n } car,\n nil cdr\n } |cons cdr\n } |cons dict\n } |trie\n >\n > |node,\n /dict <\n {\n .dict /nil if,\n {\n {\n .char char,\n { .key prefix, 1 size, .val |val val, nil dict } |trie trie\n } car,\n nil cdr\n } |cons then\n } .then,\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n {\n {\n .char char,\n {\n .key key,\n .val val,\n .dict /cons .car .trie object\n } |node _put_insert /node trie\n } car,\n .dict /cons .cdr cdr\n } |cons then\n } .then,\n {\n .dict /cons .car car,\n {\n .char char,\n .key key,\n .val val,\n .dict /cons .cdr dict\n } |dict _put_insert /dict cdr\n } |cons\n > |dict\n>;\n\n-- Insert or replace { key, val, object } -> object\nput = ?{ K key, V val, O object } {\n .key str_list? key,\n .val json? val,\n .object ptrie_obj? object\n} <\n {\n { .key key, .object object } has_key? if,\n { .key key, .val val, .object object } |node _put_replace /node then\n } .then,\n { .key key, .val val, .object object } |node _put_insert /node\n> ptrie_obj?;\n\n\n",
18
- "Examples/djson-ptrie.k": "-- Examples/djson-ptrie.k: Patricia/Radix Trie JSON schema & helpers\n-- Works with codecs/djson-ptrie.mjs (trie-array style).\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nbits[] = < {} _, B 0, B 1 > = B;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nopt[V] = < {} nil, V val >;\n\nptrie_list[V] = <\n {} nil,\n {\n bits[] prefix,\n bits[] size,\n opt[V] val,\n L 0,\n L 1\n } trie\n> = L;\n\npair[A, B] = { A char, B trie };\nlist[X] = < {} nil, { X car, D cdr } cons > = D;\n\nptrie_obj[V] = <\n {} nil,\n {\n string[] prefix,\n bits[] size,\n opt[V] val,\n list[pair[unicode[], O]] dict\n } trie\n> = O;\n\ndjson_ptrie[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n ptrie_list[J] list,\n ptrie_obj[J] object\n> = J;\n\njson? = ? djson_ptrie[];\n\n-----------------------------------------------------------------\n-- §2. Unicode Character Equality (eq_char?)\n---------------------------------------------------------------\n\neq_bit? = ?{ <{} 0, {} 1>=B x, B y }\n < {.x /0 x, .y /0 y}, {.x /1 x, .y /1 y} > {};\n\neq_bits1? = { {.x .0 x, .y .0 y} eq_bit? 0 } {};\neq_bits2? = { {.x x, .y y} eq_bits1? a, {.x .1 x, .y .1 y} eq_bit? 1 } {};\neq_bits3? = { {.x x, .y y} eq_bits2? a, {.x .2 x, .y .2 y} eq_bit? 2 } {};\neq_bits4? = { {.x x, .y y} eq_bits3? a, {.x .3 x, .y .3 y} eq_bit? 3 } {};\neq_bits7? = {\n {.x x, .y y} eq_bits4? a,\n {.x .4 x, .y .4 y} eq_bit? 4,\n {.x .5 x, .y .5 y} eq_bit? 5,\n {.x .6 x, .y .6 y} eq_bit? 6\n} {};\neq_bits8? = { {.x x, .y y} eq_bits7? a, {.x .7 x, .y .7 y} eq_bit? 7 } {};\neq_bits11? = {\n {.x x, .y y} eq_bits8? a,\n {.x .8 x, .y .8 y} eq_bit? 8,\n {.x .9 x, .y .9 y} eq_bit? 9,\n {.x .10 x, .y .10 y} eq_bit? 10\n} {};\n\neq_bmp_common_hi? = <\n {.x /h08_0F x, .y /h08_0F y} eq_bits3?,\n {.x /h10_7F x, .y /h10_7F y} eq_bits7?,\n {.x /h80_CF x, .y /h80_CF y} eq_bits7?,\n {.x /hD0_D7 x, .y /hD0_D7 y} eq_bits3?,\n {.x /hF9 x, .y /hF9 y} {},\n {.x /hFA_FB x, .y /hFA_FB y} eq_bits1?,\n {.x /hFC_FD x, .y /hFC_FD y} eq_bits1?,\n {.x /hFE_FF x, .y /hFE_FF y} eq_bits1?\n>;\n\neq_bmp_private_use_hi? = <\n {.x /hE0_EF x, .y /hE0_EF y} eq_bits4?,\n {.x /hF0_F7 x, .y /hF0_F7 y} eq_bits3?,\n {.x /hF8 x, .y /hF8 y} {}\n>;\n\neq_plane2_16? = <\n {.x /p02_03 x, .y /p02_03 y} eq_bits1?,\n {.x /p04_07 x, .y /p04_07 y} eq_bits2?,\n {.x /p08_0F x, .y /p08_0F y} eq_bits3?,\n {.x /p10 x, .y /p10 y} {}\n>;\n\neq_char? = <\n {.x /ascii x, .y /ascii y} eq_bits7?,\n {\n {.x /bmp_common .hi x, .y /bmp_common .hi y} eq_bmp_common_hi? hi,\n {.x /bmp_common .lo x, .y /bmp_common .lo y} eq_bits8? lo\n } {},\n {\n {\n .x /bmp_private_use .hi x,\n .y /bmp_private_use .hi y\n } eq_bmp_private_use_hi? hi,\n {.x /bmp_private_use .lo x, .y /bmp_private_use .lo y} eq_bits8? lo\n } {},\n {.x /plane0 x, .y /plane0 y} eq_bits11?,\n {\n {\n .x /supplementary_plane1 .mid x,\n .y /supplementary_plane1 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_plane1 .lo x,\n .y /supplementary_plane1 .lo y\n } eq_bits8? lo\n } {},\n {\n {\n .x /supplementary_planes2_16 .plane x,\n .y /supplementary_planes2_16 .plane y\n } eq_plane2_16? plane,\n {\n .x /supplementary_planes2_16 .mid x,\n .y /supplementary_planes2_16 .mid y\n } eq_bits8? mid,\n {\n .x /supplementary_planes2_16 .lo x,\n .y /supplementary_planes2_16 .lo y\n } eq_bits8? lo\n } {}\n>;\n\n---------------------------------------------------------------\n-- §3. Patricia/Radix Trie Lookup Relations (dot, nth, has_key?)\n---------------------------------------------------------------\n\nbits? = ?< {} _, bits? 0, bits? 1 > = bits?;\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\nstr_list? = ?< {} nil, { C car, S cdr } cons > = S;\nptrie_list? = ?<\n {} nil,\n { B1 prefix, B2 size, < {} nil, V val > val, T 0, T 1 } trie\n> = T;\nptrie_obj? = ?<\n {} nil,\n {\n S prefix,\n B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n> = T;\nptrie_dict? = ?<\n {} nil,\n { { C char, T trie } car, D cdr } cons\n> = D;\n\nnil = {} | nil;\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = | number json?;\nstring = | string json?;\nbool = | bool json?;\nlist = | list json?;\nobject = | object json?;\n\n-- O(1) subtree size queries (number of elements in list or object)\nlist_size = ptrie_list? < /nil 0, /trie .size >;\nobj_size = ptrie_obj? < /nil 0, /trie .size >;\n\nmatch_str_prefix = ?{ P prefix, K key } {\n .prefix str_list? prefix,\n .key str_list? key\n} <\n { .prefix /nil if, .key then } .then,\n {\n { .prefix /cons .car x, .key /cons .car y } eq_char? if,\n { .prefix /cons .cdr prefix, .key /cons .cdr key }\n match_str_prefix then\n } .then\n>;\n\nfind_in_dict = ?{ C char, D dict } {\n .char char,\n .dict ptrie_dict? dict\n} <\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n .dict /cons .car .trie then\n } .then,\n { .char char, .dict /cons .cdr dict } find_in_dict\n>;\n\n-- Lookup string key in radix object trie: { key, object } -> val\ndot = ?{ K key, T object } {\n { .object ptrie_obj? /trie .prefix prefix, .key str_list? key }\n match_str_prefix rem,\n .object ptrie_obj? /trie t\n} <\n { .rem /nil if, .t .val /val then } .then,\n {\n .rem /cons .cdr key,\n { .rem /cons .car char, .t .dict dict } find_in_dict object\n } dot\n>;\n\nhas_key? = ?{ K key, O object } { dot if, () then } .then;\n\n-- Lookup bits? index in Patricia bit-trie list: { index, list } -> val\nmatch_bit_prefix = ?{ P prefix, K key } {\n .prefix bits? prefix,\n .key bits? key\n} <\n { .prefix /_ if, .key then } .then,\n { .prefix /0 prefix, .key /0 key } match_bit_prefix,\n { .prefix /1 prefix, .key /1 key } match_bit_prefix\n>;\n\nnth = ?{ K index, T list } {\n { .list ptrie_list? /trie .prefix prefix, .index bits? key }\n match_bit_prefix rem,\n .list ptrie_list? /trie t\n} <\n { .rem /_ if, .t .val /val then } .then,\n { .rem /0 index, .t .0 list } nth,\n { .rem /1 index, .t .1 list } nth\n>;\n\n-- Binary natural number helpers for size maintenance\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_dec = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /1 x, .y |0 r } _inv,\n { .x /0 x, .y |1 y } _dec\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\n_trim0 = bits? < /0 _trim0, /_ 0, () >;\ndec_nat = { { () x, 0b r } _inv x, 0b y } _dec _trim0;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\n-- Remove key from radix object trie: { key, object } -> object\n_remove_step = ?<\n {\n < {} nil, { C car, S cdr } cons >=S key,\n <\n {} nil,\n {\n S prefix,\n < {} _, B 0, B 1 >=B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n >=T object\n } node,\n { C char, S key, D dict } dict\n> <\n /node {\n { .object /trie .prefix prefix, .key key } match_str_prefix rem,\n .object /trie t\n } <\n {\n .rem /nil if,\n {\n .t .prefix prefix,\n .t .size dec_nat size,\n nil val,\n .t .dict dict\n } |trie then\n } .then,\n {\n .t .prefix prefix,\n .t .size dec_nat size,\n .t .val val,\n {\n .rem /cons .car char,\n .rem /cons .cdr key,\n .t .dict dict\n } |dict _remove_step /dict dict\n } |trie\n > |node,\n /dict <\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n {\n {\n .char char,\n {\n .key key,\n .dict /cons .car .trie object\n } |node _remove_step /node trie\n } car,\n .dict /cons .cdr cdr\n } |cons then\n } .then,\n {\n .dict /cons .car car,\n {\n .char char,\n .key key,\n .dict /cons .cdr dict\n } |dict _remove_step /dict cdr\n } |cons\n > |dict\n>;\n\nremove = ?{ K key, O object } {\n .key str_list? key,\n .object ptrie_obj? object\n} <\n {\n { .key key, .object object } has_key? if,\n { .key key, .object object } |node _remove_step /node then\n } .then,\n .object\n> ptrie_obj?;\n\n-- Split two strings into { common, rem_prefix, rem_key }\nsplit_str_prefix = ?{\n < {} nil, { C car, S cdr } cons >=S prefix,\n S key\n} <\n {\n .prefix /nil if,\n { nil common, nil rem_prefix, .key rem_key } then\n } .then,\n {\n .key /nil if,\n { nil common, .prefix rem_prefix, nil rem_key } then\n } .then,\n {\n { .prefix /cons .car x, .key /cons .car y } eq_char? if,\n {\n .prefix /cons .car c,\n {\n .prefix /cons .cdr prefix,\n .key /cons .cdr key\n } split_str_prefix sub\n } {\n { .c car, .sub .common cdr } |cons common,\n .sub .rem_prefix rem_prefix,\n .sub .rem_key rem_key\n } then\n } .then,\n { nil common, .prefix rem_prefix, .key rem_key }\n> ?{ S1 common, S2 rem_prefix, S3 rem_key } {\n .common str_list? common,\n .rem_prefix str_list? rem_prefix,\n .rem_key str_list? rem_key\n};\n\n-- Replace value of an existing key in radix object trie\n_put_replace = ?<\n {\n < {} nil, { C car, S cdr } cons >=S key,\n V val,\n <\n {} nil,\n {\n S prefix,\n < {} _, B 0, B 1 >=B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n >=T object\n } node,\n { C char, S key, V val, D dict } dict\n> <\n /node {\n { .object /trie .prefix prefix, .key key } match_str_prefix rem,\n .val val,\n .object /trie t\n } <\n {\n .rem /nil if,\n {\n .t .prefix prefix,\n .t .size size,\n .val |val val,\n .t .dict dict\n } |trie then\n } .then,\n {\n .t .prefix prefix,\n .t .size size,\n .t .val val,\n {\n .rem /cons .car char,\n .rem /cons .cdr key,\n .val val,\n .t .dict dict\n } |dict _put_replace /dict dict\n } |trie\n > |node,\n /dict <\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n {\n {\n .char char,\n {\n .key key,\n .val val,\n .dict /cons .car .trie object\n } |node _put_replace /node trie\n } car,\n .dict /cons .cdr cdr\n } |cons then\n } .then,\n {\n .dict /cons .car car,\n {\n .char char,\n .key key,\n .val val,\n .dict /cons .cdr dict\n } |dict _put_replace /dict cdr\n } |cons\n > |dict\n>;\n\n-- Insert a new key into radix object trie (splitting prefixes as needed)\n_put_insert = ?<\n {\n < {} nil, { C car, S cdr } cons >=S key,\n V val,\n <\n {} nil,\n {\n S prefix,\n < {} _, B 0, B 1 >=B size,\n < {} nil, V val > val,\n < {} nil, { { C char, T trie } car, D cdr } cons >=D dict\n } trie\n >=T object\n } node,\n { C char, S key, V val, D dict } dict\n> <\n /node <\n {\n .object /nil if,\n { .key prefix, 1 size, .val |val val, nil dict } |trie then\n } .then,\n {\n { .object /trie .prefix prefix, .key key } split_str_prefix sp,\n .val val,\n .object /trie t\n } <\n {\n { .sp .rem_prefix /nil p, .sp .rem_key /nil k } if,\n {\n .t .prefix prefix,\n .t .size inc_nat size,\n .val |val val,\n .t .dict dict\n } |trie then\n } .then,\n {\n { .sp .rem_prefix /nil p, .sp .rem_key /cons k } if,\n {\n .t .prefix prefix,\n .t .size inc_nat size,\n .t .val val,\n {\n .sp .rem_key /cons .car char,\n .sp .rem_key /cons .cdr key,\n .val val,\n .t .dict dict\n } |dict _put_insert /dict dict\n } |trie then\n } .then,\n {\n { .sp .rem_prefix /cons p, .sp .rem_key /nil k } if,\n {\n .sp .common prefix,\n .t .size inc_nat size,\n .val |val val,\n {\n {\n .sp .rem_prefix /cons .car char,\n {\n .sp .rem_prefix /cons .cdr prefix,\n .t .size size,\n .t .val val,\n .t .dict dict\n } |trie trie\n } car,\n nil cdr\n } |cons dict\n } |trie then\n } .then,\n {\n .sp .common prefix,\n .t .size inc_nat size,\n nil val,\n {\n {\n .sp .rem_prefix /cons .car char,\n {\n .sp .rem_prefix /cons .cdr prefix,\n .t .size size,\n .t .val val,\n .t .dict dict\n } |trie trie\n } car,\n {\n {\n .sp .rem_key /cons .car char,\n {\n .sp .rem_key /cons .cdr prefix,\n 1 size,\n .val |val val,\n nil dict\n } |trie trie\n } car,\n nil cdr\n } |cons cdr\n } |cons dict\n } |trie\n >\n > |node,\n /dict <\n {\n .dict /nil if,\n {\n {\n .char char,\n { .key prefix, 1 size, .val |val val, nil dict } |trie trie\n } car,\n nil cdr\n } |cons then\n } .then,\n {\n { .char x, .dict /cons .car .char y } eq_char? if,\n {\n {\n .char char,\n {\n .key key,\n .val val,\n .dict /cons .car .trie object\n } |node _put_insert /node trie\n } car,\n .dict /cons .cdr cdr\n } |cons then\n } .then,\n {\n .dict /cons .car car,\n {\n .char char,\n .key key,\n .val val,\n .dict /cons .cdr dict\n } |dict _put_insert /dict cdr\n } |cons\n > |dict\n>;\n\n-- Insert or replace { key, val, object } -> object\nput = ?{ K key, V val, O object } {\n .key str_list? key,\n .val json? val,\n .object ptrie_obj? object\n} <\n {\n { .key key, .object object } has_key? if,\n { .key key, .val val, .object object } |node _put_replace /node then\n } .then,\n { .key key, .val val, .object object } |node _put_insert /node\n> ptrie_obj?;\n\n\n",
13
+ "djson.k": "-- Examples/djson.k: Universal dynamic JSON schema and standard methods\n-- Works with codecs/djson.mjs (k-djson) and codecs/utf8.mjs (k-utf8).\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nbits[] = < {} _, B 0, B 1 > = B;\n\nlist[X] = < {} nil, { X car, L cdr } cons > = L;\n\nopt[V] = < {} nil, V val >;\n\nbtrie[V] = <\n {} nil,\n {\n opt[V] val,\n T 0,\n T 1\n } trie\n> = T;\n\ndjson[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n btrie[J] list,\n btrie[J] object\n> = J;\n\ndjson_btrie[] = djson[];\n\njson? = ? djson[];\n\n---------------------------------------------------------------\n-- §2. Constructors and Projections\n---------------------------------------------------------------\n\nbits? = ? bits[];\nstr_list? = ? string[];\ntrie? = ? btrie[V];\n\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\n\nnil = {} | nil;\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = | number json?;\nstring = | string json?;\nbool = | bool json?;\nlist = | list json?;\nobject = | object json?;\nempty_list = nil | list json?;\nempty_object = nil | object json?;\n\n---------------------------------------------------------------\n-- §3. Direct Binary Bit-Trie Traversal (Zero Equality Checks)\n---------------------------------------------------------------\n\n-- Step one bit (0 or 1) in a binary trie\nstep_0 = /trie .0;\nstep_1 = /trie .1;\n\nstep_bit = <\n { .b /0 if, .t step_0 then } .then,\n { .b /1 if, .t step_1 then } .then\n>;\n\n-- Step N bits from MSB (.N-1) down to LSB (.0)\nstep_bits1 = { .x .0 b, .t t } step_bit;\nstep_bits2 = { .x x, { .x .1 b, .t t } step_bit t } step_bits1;\nstep_bits3 = { .x x, { .x .2 b, .t t } step_bit t } step_bits2;\nstep_bits4 = { .x x, { .x .3 b, .t t } step_bit t } step_bits3;\nstep_bits5 = { .x x, { .x .4 b, .t t } step_bit t } step_bits4;\nstep_bits6 = { .x x, { .x .5 b, .t t } step_bit t } step_bits5;\nstep_bits7 = { .x x, { .x .6 b, .t t } step_bit t } step_bits6;\nstep_bits8 = { .x x, { .x .7 b, .t t } step_bit t } step_bits7;\nstep_bits9 = { .x x, { .x .8 b, .t t } step_bit t } step_bits8;\nstep_bits10 = { .x x, { .x .9 b, .t t } step_bit t } step_bits9;\nstep_bits11 = { .x x, { .x .10 b, .t t } step_bit t } step_bits10;\n\nstep_bmp_common_hi = <\n { .hi /h08_0F x, .t step_0 step_0 step_0 t } step_bits3,\n { .hi /h10_7F x, .t step_0 step_0 step_1 t } step_bits7,\n { .hi /h80_CF x, .t step_0 step_1 step_0 t } step_bits7,\n { .hi /hD0_D7 x, .t step_0 step_1 step_1 t } step_bits3,\n { .hi /hF9 if, .t step_1 step_0 step_0 then } .then,\n { .hi /hFA_FB x, .t step_1 step_0 step_1 t } step_bits1,\n { .hi /hFC_FD x, .t step_1 step_1 step_0 t } step_bits1,\n { .hi /hFE_FF x, .t step_1 step_1 step_1 t } step_bits1\n>;\n\nstep_bmp_priv_hi = <\n { .hi /hE0_EF x, .t step_0 t } step_bits4,\n { .hi /hF0_F7 x, .t step_1 step_0 t } step_bits3,\n { .hi /hF8 if, .t step_1 step_1 then } .then\n>;\n\nstep_plane2_16 = <\n { .p /p02_03 x, .t step_0 step_0 t } step_bits1,\n { .p /p04_07 x, .t step_0 step_1 t } step_bits2,\n { .p /p08_0F x, .t step_1 step_0 t } step_bits3,\n { .p /p10 if, .t step_1 step_1 then } .then\n>;\n\n-- Advance trie by one Unicode character: { char, t } -> t\nstep_char = <\n { .char /ascii x, .t step_0 t } step_bits7,\n { .char /plane0 x, .t step_1 step_0 t } step_bits11,\n {\n .char /bmp_common .lo x,\n { .char /bmp_common .hi hi, .t step_1 step_1 step_0 t }\n step_bmp_common_hi t\n } step_bits8,\n {\n .char /bmp_private_use .lo x,\n { .char /bmp_private_use .hi hi, .t step_1 step_1 step_1 step_0 t }\n step_bmp_priv_hi t\n } step_bits8,\n {\n .char /supplementary_plane1 .lo x,\n {\n .char /supplementary_plane1 .mid x,\n .t step_1 step_1 step_1 step_1 step_0 t\n } step_bits8 t\n } step_bits8,\n {\n .char /supplementary_planes2_16 .lo x,\n {\n .char /supplementary_planes2_16 .mid x,\n {\n .char /supplementary_planes2_16 .plane p,\n .t step_1 step_1 step_1 step_1 step_1 t\n } step_plane2_16 t\n } step_bits8 t\n } step_bits8\n>;\n\n---------------------------------------------------------------\n-- §4. Object & List Lookup Relations (dot, nth, has_key?)\n---------------------------------------------------------------\n\n-- Low-level lookup string key in object trie: { key, object } -> val\ndot = ?{ K key, T object } { .key str_list? key, .object trie? t } <\n { .key /nil if, .t /trie .val /val then } .then,\n {\n .key /cons .cdr key,\n { .key /cons .car char, .t t } step_char object\n } dot\n>;\n\n-- Low-level lookup bits? index in list trie: { index, list } -> val\n_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .index /_ if, .t /trie .val /val then } .then,\n { .index /0 index, .t /trie .0 list } _nth,\n { .index /1 index, .t /trie .1 list } _nth\n>;\n\n-- Primary accessor: lookup string key in json? object -> json? val\nget = ?{ K key, json? json } {\n .key str_list? key,\n .json /object object\n} dot;\n\n-- Primary accessor: lookup bits? index in json? list -> json? elem\nnth = ?{ K index, json? json } {\n .index bits? index,\n .json /list list\n} _nth;\n\n-- Filter: succeeds as identity if json? object contains string key\nhas_key? = ?{ K key, json? json } { get if, () then } .then;\nhas? = has_key?;\n\n-- Filter: succeeds as identity if json? list contains index\nhas_index? = ?{ K index, json? json } { nth if, () then } .then;\nhas_nth? = has_index?;\n\n---------------------------------------------------------------\n-- §5. Trie Update & Removal (put, remove, put_nth, remove_nth)\n---------------------------------------------------------------\n\npush_bit = ?{ < {} 0, {} 1 > b, < {} _, BITS 0, BITS 1 >=BITS acc } <\n { .b /0 if, .acc |0 then } .then,\n { .b /1 if, .acc |1 then } .then\n> bits?;\n\npush_bits1 = { .x .0 b, .acc acc } push_bit;\npush_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;\npush_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;\npush_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;\npush_bits5 = { .x .4 b, { .x x, .acc acc } push_bits4 acc } push_bit;\npush_bits6 = { .x .5 b, { .x x, .acc acc } push_bits5 acc } push_bit;\npush_bits7 = { .x .6 b, { .x x, .acc acc } push_bits6 acc } push_bit;\npush_bits8 = { .x .7 b, { .x x, .acc acc } push_bits7 acc } push_bit;\npush_bits9 = { .x .8 b, { .x x, .acc acc } push_bits8 acc } push_bit;\npush_bits10 = { .x .9 b, { .x x, .acc acc } push_bits9 acc } push_bit;\npush_bits11 = { .x .10 b, { .x x, .acc acc } push_bits10 acc } push_bit;\n\npush_bmp_common_hi = <\n { .hi /h08_0F x, .acc acc } push_bits3 |0 |0 |0,\n { .hi /h10_7F x, .acc acc } push_bits7 |1 |0 |0,\n { .hi /h80_CF x, .acc acc } push_bits7 |0 |1 |0,\n { .hi /hD0_D7 x, .acc acc } push_bits3 |1 |1 |0,\n { .hi /hF9 if, .acc |0 |0 |1 then } .then,\n { .hi /hFA_FB x, .acc acc } push_bits1 |1 |0 |1,\n { .hi /hFC_FD x, .acc acc } push_bits1 |0 |1 |1,\n { .hi /hFE_FF x, .acc acc } push_bits1 |1 |1 |1\n> bits?;\n\npush_bmp_priv_hi = <\n { .hi /hE0_EF x, .acc acc } push_bits4 |0,\n { .hi /hF0_F7 x, .acc acc } push_bits3 |0 |1,\n { .hi /hF8 if, .acc |1 |1 then } .then\n> bits?;\n\npush_plane2_16 = <\n { .p /p02_03 x, .acc acc } push_bits1 |0 |0,\n { .p /p04_07 x, .acc acc } push_bits2 |1 |0,\n { .p /p08_0F x, .acc acc } push_bits3 |0 |1,\n { .p /p10 if, .acc |1 |1 then } .then\n> bits?;\n\nprepend_char_bits = <\n { .char /ascii x, .acc acc } push_bits7 |0,\n { .char /plane0 x, .acc acc } push_bits11 |0 |1,\n {\n .char /bmp_common .hi hi,\n { .char /bmp_common .lo x, .acc acc } push_bits8 acc\n } push_bmp_common_hi |0 |1 |1,\n {\n .char /bmp_private_use .hi hi,\n { .char /bmp_private_use .lo x, .acc acc } push_bits8 acc\n } push_bmp_priv_hi |0 |1 |1 |1,\n {\n .char /supplementary_plane1 .mid x,\n { .char /supplementary_plane1 .lo x, .acc acc } push_bits8 acc\n } push_bits8 |0 |1 |1 |1 |1,\n {\n .char /supplementary_planes2_16 .plane p,\n {\n .char /supplementary_planes2_16 .mid x,\n { .char /supplementary_planes2_16 .lo x, .acc acc } push_bits8 acc\n } push_bits8 acc\n } push_plane2_16 |1 |1 |1 |1 |1\n> bits?;\n\nstr_to_bits = str_list? <\n /nil 0b,\n /cons { .car char, .cdr str_to_bits acc } prepend_char_bits\n> bits?;\n\nensure_trie = trie? < /nil { nil val, nil 0, nil 1 }, /trie () >;\n\n-- Insert or replace { index, val, list } -> list\nput_nth = ?{ K index, V val, T list } {\n .index bits? index,\n .val val,\n .list trie? ensure_trie t\n} <\n {\n .index /_ if,\n { .val |val val, .t .0 0, .t .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t .val val,\n { .index /0 index, .val val, .t .0 list } put_nth 0,\n .t .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t .val val,\n .t .0 0,\n { .index /1 index, .val val, .t .1 list } put_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Remove entry at bits? index from trie: { index, list } -> list\nremove_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .t /nil if, nil then } .then,\n {\n .index /_ if,\n { nil val, .t /trie .0 0, .t /trie .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t /trie .val val,\n { .index /0 index, .t /trie .0 list } remove_nth 0,\n .t /trie .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t /trie .val val,\n .t /trie .0 0,\n { .index /1 index, .t /trie .1 list } remove_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Insert or replace { key, val, object } -> object\nput = ?{ K key, V val, O object } {\n .key str_to_bits index,\n .val val,\n .object list\n} put_nth;\n\n-- Remove key from object trie: { key, object } -> object\nremove = ?{ K key, O object } {\n .key str_to_bits index,\n .object list\n} remove_nth;\n\n-- Set string key in json? object: { key, val, json } -> json\nset = ?{ K key, json? val, json? json } {\n .key str_list? key,\n .val val,\n .json /object object\n} put |object json?;\n\n-- Remove string key from json? object: { key, json } -> json\ndel = ?{ K key, json? json } {\n .key str_list? key,\n .json /object object\n} remove |object json?;\n\n-- Set element at bits? index in json? list: { index, val, json } -> json\nset_nth = ?{ K index, json? val, json? json } {\n .index bits? index,\n .val val,\n .json /list list\n} put_nth |list json?;\n\n-- Remove element at bits? index from json? list: { index, json } -> json\ndel_nth = ?{ K index, json? json } {\n .index bits? index,\n .json /list list\n} remove_nth |list json?;\n\n---------------------------------------------------------------\n-- §6. Cardinality & Length Relations\n---------------------------------------------------------------\n\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\n_trie_size = ?{\n < {} nil, { < {} nil, V val > val, T 0, T 1 } trie >=T t,\n < {} _, B 0, B 1 >=B acc\n} <\n { .t /nil if, .acc then } .then,\n {\n .t /trie .val /nil if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc acc } _trie_size acc\n } _trie_size then\n } .then,\n {\n .t /trie .val /val if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc inc_nat acc } _trie_size acc\n } _trie_size then\n } .then\n> bits?;\n\nlist_size = { trie? t, 0 acc } _trie_size;\nobj_size = { trie? t, 0 acc } _trie_size;\n\n_str_len = ?{ string[] str, bits[] acc } <\n { .str /nil if, .acc then } .then,\n { .str /cons .cdr str, .acc inc_nat acc } _str_len\n> ? bits[];\nstr_len = { str_list? str, 0 acc } _str_len;\n\n-- Count items in json? (object entries, list elements, or string chars)\nlength_nat = json? <\n /object obj_size,\n /list list_size,\n /string str_len\n>;\n\npos_sign = {} |\"+\";\nbit0 = {} |0;\nbit1 = {} |1;\n\npos_zero = {\n pos_sign sign,\n {\n bit0 0, bit0 1, bit0 2, bit0 3, bit0 4,\n bit0 5, bit0 6, bit0 7, bit0 8, bit0 9, bit0 10\n } exponent,\n {\n bit0 0, bit0 1, bit0 2, bit0 3, bit0 4,\n bit0 5, bit0 6, bit0 7, bit0 8, bit0 9,\n bit0 10, bit0 11, bit0 12, bit0 13, bit0 14,\n bit0 15, bit0 16, bit0 17, bit0 18, bit0 19,\n bit0 20, bit0 21, bit0 22, bit0 23, bit0 24,\n bit0 25, bit0 26, bit0 27, bit0 28, bit0 29,\n bit0 30, bit0 31, bit0 32, bit0 33, bit0 34,\n bit0 35, bit0 36, bit0 37, bit0 38, bit0 39,\n bit0 40, bit0 41, bit0 42, bit0 43, bit0 44,\n bit0 45, bit0 46, bit0 47, bit0 48, bit0 49,\n bit0 50, bit0 51\n } fraction\n};\n\nexp_bias = {\n bit1 0, bit1 1, bit1 2, bit1 3, bit1 4,\n bit1 5, bit1 6, bit1 7, bit1 8, bit1 9, bit0 10\n};\n\nexp_half_adder = <\n { .a /0 a, .c_in /0 c_in } { bit0 sum, bit0 carry },\n { .a /0 a, .c_in /1 c_in } { bit1 sum, bit0 carry },\n { .a /1 a, .c_in /0 c_in } { bit1 sum, bit0 carry },\n { .a /1 a, .c_in /1 c_in } { bit0 sum, bit1 carry }\n>;\n\ninc_exp = {\n { .0 a, bit1 c_in } exp_half_adder e0,\n .1 b1, .2 b2, .3 b3, .4 b4, .5 b5, .6 b6, .7 b7, .8 b8, .9 b9, .10 b10\n} {\n .e0.sum 0,\n { .b1 a, .e0.carry c_in } exp_half_adder e1,\n .b2 b2, .b3 b3, .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .e1.sum 1,\n { .b2 a, .e1.carry c_in } exp_half_adder e2,\n .b3 b3, .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .e2.sum 2,\n { .b3 a, .e2.carry c_in } exp_half_adder e3,\n .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .e3.sum 3,\n { .b4 a, .e3.carry c_in } exp_half_adder e4,\n .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .e4.sum 4,\n { .b5 a, .e4.carry c_in } exp_half_adder e5,\n .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .e5.sum 5,\n { .b6 a, .e5.carry c_in } exp_half_adder e6,\n .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .e6.sum 6,\n { .b7 a, .e6.carry c_in } exp_half_adder e7,\n .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .e7.sum 7,\n { .b8 a, .e7.carry c_in } exp_half_adder e8,\n .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .e8.sum 8,\n { .b9 a, .e8.carry c_in } exp_half_adder e9,\n .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .8 8, .e9.sum 9,\n { .b10 a, .e9.carry c_in } exp_half_adder e10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .8 8, .9 9, .e10.sum 10\n};\n\nshift_frac = ?{ bit[] b, fraction[] f } {\n .f.1 0, .f.2 1, .f.3 2, .f.4 3, .f.5 4,\n .f.6 5, .f.7 6, .f.8 7, .f.9 8, .f.10 9,\n .f.11 10, .f.12 11, .f.13 12, .f.14 13, .f.15 14,\n .f.16 15, .f.17 16, .f.18 17, .f.19 18, .f.20 19,\n .f.21 20, .f.22 21, .f.23 22, .f.24 23, .f.25 24,\n .f.26 25, .f.27 26, .f.28 27, .f.29 28, .f.30 29,\n .f.31 30, .f.32 31, .f.33 32, .f.34 33, .f.35 34,\n .f.36 35, .f.37 36, .f.38 37, .f.39 38, .f.40 39,\n .f.41 40, .f.42 41, .f.43 42, .f.44 43, .f.45 44,\n .f.46 45, .f.47 46, .f.48 47, .f.49 48, .f.50 49,\n .f.51 50, .b 51\n};\n\n_nat_step = ?{ bits[] bits, exponent[] exp, fraction[] frac } <\n {\n .bits /_ if,\n { pos_sign sign, .exp exponent, .frac fraction } then\n } .then,\n {\n .bits /0 bits,\n .exp inc_exp exp,\n { bit0 b, .frac f } shift_frac frac\n } _nat_step,\n {\n .bits /1 bits,\n .exp inc_exp exp,\n { bit1 b, .frac f } shift_frac frac\n } _nat_step\n>;\n\ninv_bits = { () x, 0b r } _inv;\n\nnat_to_float64 = bits? <\n /_ { pos_zero then } .then,\n /0 { pos_zero then } .then,\n /1 { () inv_bits bits, exp_bias exp, pos_zero .fraction frac } _nat_step\n>;\n\n-- Count items in json? -> json? number\nlength = length_nat nat_to_float64 |number json?;\n\n-- Append element to json? list: { val, json } -> json\npush = ?{ json? val, json? json } {\n (.json /list list_size) index,\n .val val,\n .json json\n} set_nth;\n\n---------------------------------------------------------------\n-- §7. Keys & Values Extraction\n---------------------------------------------------------------\n\nread_bit = bits? <\n /0 { {} |0 bit, () rest },\n /1 { {} |1 bit, () rest }\n>;\n\nread_bits7 =\n read_bit\n { .bit b6, (.rest read_bit) r5 }\n { .b6 b6, .r5 .bit b5, (.r5 .rest read_bit) r4 }\n { .b6 b6, .b5 b5, .r4 .bit b4, (.r4 .rest read_bit) r3 }\n { .b6 b6, .b5 b5, .b4 b4, .r3 .bit b3, (.r3 .rest read_bit) r2 }\n { .b6 b6, .b5 b5, .b4 b4, .b3 b3, .r2 .bit b2, (.r2 .rest read_bit) r1 }\n {\n .b6 b6, .b5 b5, .b4 b4, .b3 b3, .b2 b2,\n .r1 .bit b1, (.r1 .rest read_bit) r0\n }\n {\n { .r0 .bit 0, .b1 1, .b2 2, .b3 3, .b4 4, .b5 5, .b6 6 } |ascii val,\n .r0 .rest rest\n };\n\nread_char = bits? <\n /0 read_bits7\n>;\n\nbits_to_str = bits? <\n /_ {} |nil,\n read_char { .val car, (.rest bits_to_str) cdr } |cons\n> str_list?;\n\n_collect_keys = ?{\n btrie[V] tree,\n bits[] rev_path,\n list[string[]] acc\n} <\n { .tree /nil if, .acc then } .then,\n {\n .tree /trie .val /nil if,\n {\n .tree /trie .0 tree,\n .rev_path |0 rev_path,\n {\n .tree /trie .1 tree,\n .rev_path |1 rev_path,\n .acc acc\n } _collect_keys acc\n } _collect_keys then\n } .then,\n {\n .tree /trie .val /val if,\n {\n .tree /trie .0 tree,\n .rev_path |0 rev_path,\n {\n .tree /trie .1 tree,\n .rev_path |1 rev_path,\n {\n (.rev_path inv_bits bits_to_str) car,\n .acc cdr\n } |cons acc\n } _collect_keys acc\n } _collect_keys then\n } .then\n> ? list[string[]];\n\n_obj_keys_raw = { () tree, 0b rev_path, {} |nil acc } _collect_keys;\n\n-- Extract all keys from json? object as raw list: json? -> list[string[]]\nkeys_raw = json? /object _obj_keys_raw;\n\n_keys_to_trie = ?{\n list[string[]] klist,\n bits[] index,\n btrie[djson_btrie[]] acc\n} <\n { .klist /nil if, .acc then } .then,\n {\n .klist /cons .cdr klist,\n .index inc_nat index,\n {\n .index index,\n .klist /cons .car |string val,\n .acc list\n } put_nth acc\n } _keys_to_trie\n>;\n\n-- Extract all keys from json? object as a json? list: json? -> json?\nkeys = json? /object _obj_keys_raw\n { () klist, 0 index, {} |nil acc } _keys_to_trie\n |list json?;\n\n_collect_values = ?{\n btrie[V] tree,\n list[V] acc\n} <\n { .tree /nil if, .acc then } .then,\n {\n .tree /trie .val /nil if,\n {\n .tree /trie .0 tree,\n { .tree /trie .1 tree, .acc acc } _collect_values acc\n } _collect_values then\n } .then,\n {\n .tree /trie .val /val if,\n {\n .tree /trie .0 tree,\n {\n .tree /trie .1 tree,\n { .tree /trie .val /val car, .acc cdr } |cons acc\n } _collect_values acc\n } _collect_values then\n } .then\n> ? list[V];\n\n_obj_values_raw = { () tree, {} |nil acc } _collect_values;\n\n-- Extract all values from json? object or list as raw list\nvalues_raw = json? <\n /object _obj_values_raw,\n /list _obj_values_raw\n>;\n\n_values_to_trie = ?{\n list[djson_btrie[]] klist,\n bits[] index,\n btrie[djson_btrie[]] acc\n} <\n { .klist /nil if, .acc then } .then,\n {\n .klist /cons .cdr klist,\n .index inc_nat index,\n {\n .index index,\n .klist /cons .car val,\n .acc list\n } put_nth acc\n } _values_to_trie\n>;\n\n-- Extract all values from json? as a json? list: json? -> json?\nvalues = <\n json? /object _obj_values_raw\n { () klist, 0 index, {} |nil acc } _values_to_trie\n |list json?,\n json? /list |list json?\n>;\n\n---------------------------------------------------------------\n-- §8. Type Predicates\n---------------------------------------------------------------\n\nis_null? = json? /null ();\nis_bool? = json? /bool ();\nis_number? = json? /number ();\nis_string? = json? /string ();\nis_list? = json? /list ();\nis_object? = json? /object ();\n\njson_type[] = <\n {} null, {} bool, {} number, {} string, {} list, {} object\n>;\ntype_of = json? <\n /null {} |null,\n /bool {} |bool,\n /number {} |number,\n /string {} |string,\n /list {} |list,\n /object {} |object\n> ? json_type[];\n",
14
+ "Examples/djson.k": "-- Examples/djson.k: Universal dynamic JSON schema and standard methods\n-- Works with codecs/djson.mjs (k-djson) and codecs/utf8.mjs (k-utf8).\n\n---------------------------------------------------------------\n-- §1. Named Pattern Definitions & Component Schemas\n---------------------------------------------------------------\n\nbit[] = < {} 0, {} 1 >;\n\nsign[] = < {} \"+\", {} \"-\" >;\n\nexponent[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nfraction[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,\n bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,\n bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,\n bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,\n bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,\n bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,\n bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,\n bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,\n bit[] 48, bit[] 49, bit[] 50, bit[] 51\n};\n\nnumber[] = {\n sign[] sign,\n exponent[] exponent,\n fraction[] fraction\n};\n\nbyte[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6, bit[] 7\n};\n\nascii[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3,\n bit[] 4, bit[] 5, bit[] 6\n};\n\nbmp_hi_common[] = <\n { bit[] 0, bit[] 1, bit[] 2 } h08_0F,\n ascii[] h10_7F,\n ascii[] h80_CF,\n { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,\n {} hF9,\n { bit[] 0 } hFA_FB,\n { bit[] 0 } hFC_FD,\n { bit[] 0 } hFE_FF\n>;\n\nbmp_hi_private_use[] = <\n { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,\n { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,\n {} hF8\n>;\n\nplane0[] = {\n bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,\n bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10\n};\n\nplane_02_10[] = <\n { bit[] 0 } p02_03,\n { bit[] 0, bit[] 1 } p04_07,\n { bit[] 0, bit[] 1, bit[] 2 } p08_0F,\n {} p10\n>;\n\nunicode[] = <\n ascii[] ascii,\n { bmp_hi_common[] hi, byte[] lo } bmp_common,\n { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,\n plane0[] plane0,\n { byte[] mid, byte[] lo } supplementary_plane1,\n { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16\n>;\n\nstring[] = < {} nil, { unicode[] car, S cdr } cons > = S;\n\nbool[] = < {} true, {} false >;\n\nbits[] = < {} _, B 0, B 1 > = B;\n\nlist[X] = < {} nil, { X car, L cdr } cons > = L;\n\nopt[V] = < {} nil, V val >;\n\nbtrie[V] = <\n {} nil,\n {\n opt[V] val,\n T 0,\n T 1\n } trie\n> = T;\n\ndjson[] = <\n number[] number,\n string[] string,\n bool[] bool,\n {} null,\n btrie[J] list,\n btrie[J] object\n> = J;\n\ndjson_btrie[] = djson[];\n\njson? = ? djson[];\n\n---------------------------------------------------------------\n-- §2. Constructors and Projections\n---------------------------------------------------------------\n\nbits? = ? bits[];\nstr_list? = ? string[];\ntrie? = ? btrie[V];\n\n0b = {} | _;\n0 = 0b | 0;\n1 = 0b | 1;\n\nnil = {} | nil;\nnull = {} | null json?;\ntrue = {} | true | bool json?;\nfalse = {} | false | bool json?;\nnumber = | number json?;\nstring = | string json?;\nbool = | bool json?;\nlist = | list json?;\nobject = | object json?;\nempty_list = nil | list json?;\nempty_object = nil | object json?;\n\n---------------------------------------------------------------\n-- §3. Direct Binary Bit-Trie Traversal (Zero Equality Checks)\n---------------------------------------------------------------\n\n-- Step one bit (0 or 1) in a binary trie\nstep_0 = /trie .0;\nstep_1 = /trie .1;\n\nstep_bit = <\n { .b /0 if, .t step_0 then } .then,\n { .b /1 if, .t step_1 then } .then\n>;\n\n-- Step N bits from MSB (.N-1) down to LSB (.0)\nstep_bits1 = { .x .0 b, .t t } step_bit;\nstep_bits2 = { .x x, { .x .1 b, .t t } step_bit t } step_bits1;\nstep_bits3 = { .x x, { .x .2 b, .t t } step_bit t } step_bits2;\nstep_bits4 = { .x x, { .x .3 b, .t t } step_bit t } step_bits3;\nstep_bits5 = { .x x, { .x .4 b, .t t } step_bit t } step_bits4;\nstep_bits6 = { .x x, { .x .5 b, .t t } step_bit t } step_bits5;\nstep_bits7 = { .x x, { .x .6 b, .t t } step_bit t } step_bits6;\nstep_bits8 = { .x x, { .x .7 b, .t t } step_bit t } step_bits7;\nstep_bits9 = { .x x, { .x .8 b, .t t } step_bit t } step_bits8;\nstep_bits10 = { .x x, { .x .9 b, .t t } step_bit t } step_bits9;\nstep_bits11 = { .x x, { .x .10 b, .t t } step_bit t } step_bits10;\n\nstep_bmp_common_hi = <\n { .hi /h08_0F x, .t step_0 step_0 step_0 t } step_bits3,\n { .hi /h10_7F x, .t step_0 step_0 step_1 t } step_bits7,\n { .hi /h80_CF x, .t step_0 step_1 step_0 t } step_bits7,\n { .hi /hD0_D7 x, .t step_0 step_1 step_1 t } step_bits3,\n { .hi /hF9 if, .t step_1 step_0 step_0 then } .then,\n { .hi /hFA_FB x, .t step_1 step_0 step_1 t } step_bits1,\n { .hi /hFC_FD x, .t step_1 step_1 step_0 t } step_bits1,\n { .hi /hFE_FF x, .t step_1 step_1 step_1 t } step_bits1\n>;\n\nstep_bmp_priv_hi = <\n { .hi /hE0_EF x, .t step_0 t } step_bits4,\n { .hi /hF0_F7 x, .t step_1 step_0 t } step_bits3,\n { .hi /hF8 if, .t step_1 step_1 then } .then\n>;\n\nstep_plane2_16 = <\n { .p /p02_03 x, .t step_0 step_0 t } step_bits1,\n { .p /p04_07 x, .t step_0 step_1 t } step_bits2,\n { .p /p08_0F x, .t step_1 step_0 t } step_bits3,\n { .p /p10 if, .t step_1 step_1 then } .then\n>;\n\n-- Advance trie by one Unicode character: { char, t } -> t\nstep_char = <\n { .char /ascii x, .t step_0 t } step_bits7,\n { .char /plane0 x, .t step_1 step_0 t } step_bits11,\n {\n .char /bmp_common .lo x,\n { .char /bmp_common .hi hi, .t step_1 step_1 step_0 t }\n step_bmp_common_hi t\n } step_bits8,\n {\n .char /bmp_private_use .lo x,\n { .char /bmp_private_use .hi hi, .t step_1 step_1 step_1 step_0 t }\n step_bmp_priv_hi t\n } step_bits8,\n {\n .char /supplementary_plane1 .lo x,\n {\n .char /supplementary_plane1 .mid x,\n .t step_1 step_1 step_1 step_1 step_0 t\n } step_bits8 t\n } step_bits8,\n {\n .char /supplementary_planes2_16 .lo x,\n {\n .char /supplementary_planes2_16 .mid x,\n {\n .char /supplementary_planes2_16 .plane p,\n .t step_1 step_1 step_1 step_1 step_1 t\n } step_plane2_16 t\n } step_bits8 t\n } step_bits8\n>;\n\n---------------------------------------------------------------\n-- §4. Object & List Lookup Relations (dot, nth, has_key?)\n---------------------------------------------------------------\n\n-- Low-level lookup string key in object trie: { key, object } -> val\ndot = ?{ K key, T object } { .key str_list? key, .object trie? t } <\n { .key /nil if, .t /trie .val /val then } .then,\n {\n .key /cons .cdr key,\n { .key /cons .car char, .t t } step_char object\n } dot\n>;\n\n-- Low-level lookup bits? index in list trie: { index, list } -> val\n_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .index /_ if, .t /trie .val /val then } .then,\n { .index /0 index, .t /trie .0 list } _nth,\n { .index /1 index, .t /trie .1 list } _nth\n>;\n\n-- Primary accessor: lookup string key in json? object -> json? val\nget = ?{ K key, json? json } {\n .key str_list? key,\n .json /object object\n} dot;\n\n-- Primary accessor: lookup bits? index in json? list -> json? elem\nnth = ?{ K index, json? json } {\n .index bits? index,\n .json /list list\n} _nth;\n\n-- Filter: succeeds as identity if json? object contains string key\nhas_key? = ?{ K key, json? json } { get if, () then } .then;\nhas? = has_key?;\n\n-- Filter: succeeds as identity if json? list contains index\nhas_index? = ?{ K index, json? json } { nth if, () then } .then;\nhas_nth? = has_index?;\n\n---------------------------------------------------------------\n-- §5. Trie Update & Removal (put, remove, put_nth, remove_nth)\n---------------------------------------------------------------\n\npush_bit = ?{ < {} 0, {} 1 > b, < {} _, BITS 0, BITS 1 >=BITS acc } <\n { .b /0 if, .acc |0 then } .then,\n { .b /1 if, .acc |1 then } .then\n> bits?;\n\npush_bits1 = { .x .0 b, .acc acc } push_bit;\npush_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;\npush_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;\npush_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;\npush_bits5 = { .x .4 b, { .x x, .acc acc } push_bits4 acc } push_bit;\npush_bits6 = { .x .5 b, { .x x, .acc acc } push_bits5 acc } push_bit;\npush_bits7 = { .x .6 b, { .x x, .acc acc } push_bits6 acc } push_bit;\npush_bits8 = { .x .7 b, { .x x, .acc acc } push_bits7 acc } push_bit;\npush_bits9 = { .x .8 b, { .x x, .acc acc } push_bits8 acc } push_bit;\npush_bits10 = { .x .9 b, { .x x, .acc acc } push_bits9 acc } push_bit;\npush_bits11 = { .x .10 b, { .x x, .acc acc } push_bits10 acc } push_bit;\n\npush_bmp_common_hi = <\n { .hi /h08_0F x, .acc acc } push_bits3 |0 |0 |0,\n { .hi /h10_7F x, .acc acc } push_bits7 |1 |0 |0,\n { .hi /h80_CF x, .acc acc } push_bits7 |0 |1 |0,\n { .hi /hD0_D7 x, .acc acc } push_bits3 |1 |1 |0,\n { .hi /hF9 if, .acc |0 |0 |1 then } .then,\n { .hi /hFA_FB x, .acc acc } push_bits1 |1 |0 |1,\n { .hi /hFC_FD x, .acc acc } push_bits1 |0 |1 |1,\n { .hi /hFE_FF x, .acc acc } push_bits1 |1 |1 |1\n> bits?;\n\npush_bmp_priv_hi = <\n { .hi /hE0_EF x, .acc acc } push_bits4 |0,\n { .hi /hF0_F7 x, .acc acc } push_bits3 |0 |1,\n { .hi /hF8 if, .acc |1 |1 then } .then\n> bits?;\n\npush_plane2_16 = <\n { .p /p02_03 x, .acc acc } push_bits1 |0 |0,\n { .p /p04_07 x, .acc acc } push_bits2 |1 |0,\n { .p /p08_0F x, .acc acc } push_bits3 |0 |1,\n { .p /p10 if, .acc |1 |1 then } .then\n> bits?;\n\nprepend_char_bits = <\n { .char /ascii x, .acc acc } push_bits7 |0,\n { .char /plane0 x, .acc acc } push_bits11 |0 |1,\n {\n .char /bmp_common .hi hi,\n { .char /bmp_common .lo x, .acc acc } push_bits8 acc\n } push_bmp_common_hi |0 |1 |1,\n {\n .char /bmp_private_use .hi hi,\n { .char /bmp_private_use .lo x, .acc acc } push_bits8 acc\n } push_bmp_priv_hi |0 |1 |1 |1,\n {\n .char /supplementary_plane1 .mid x,\n { .char /supplementary_plane1 .lo x, .acc acc } push_bits8 acc\n } push_bits8 |0 |1 |1 |1 |1,\n {\n .char /supplementary_planes2_16 .plane p,\n {\n .char /supplementary_planes2_16 .mid x,\n { .char /supplementary_planes2_16 .lo x, .acc acc } push_bits8 acc\n } push_bits8 acc\n } push_plane2_16 |1 |1 |1 |1 |1\n> bits?;\n\nstr_to_bits = str_list? <\n /nil 0b,\n /cons { .car char, .cdr str_to_bits acc } prepend_char_bits\n> bits?;\n\nensure_trie = trie? < /nil { nil val, nil 0, nil 1 }, /trie () >;\n\n-- Insert or replace { index, val, list } -> list\nput_nth = ?{ K index, V val, T list } {\n .index bits? index,\n .val val,\n .list trie? ensure_trie t\n} <\n {\n .index /_ if,\n { .val |val val, .t .0 0, .t .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t .val val,\n { .index /0 index, .val val, .t .0 list } put_nth 0,\n .t .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t .val val,\n .t .0 0,\n { .index /1 index, .val val, .t .1 list } put_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Remove entry at bits? index from trie: { index, list } -> list\nremove_nth = ?{ K index, T list } { .index bits? index, .list trie? t } <\n { .t /nil if, nil then } .then,\n {\n .index /_ if,\n { nil val, .t /trie .0 0, .t /trie .1 1 } |trie then\n } .then,\n {\n .index /0 if,\n {\n .t /trie .val val,\n { .index /0 index, .t /trie .0 list } remove_nth 0,\n .t /trie .1 1\n } |trie then\n } .then,\n {\n .index /1 if,\n {\n .t /trie .val val,\n .t /trie .0 0,\n { .index /1 index, .t /trie .1 list } remove_nth 1\n } |trie then\n } .then\n> trie?;\n\n-- Insert or replace { key, val, object } -> object\nput = ?{ K key, V val, O object } {\n .key str_to_bits index,\n .val val,\n .object list\n} put_nth;\n\n-- Remove key from object trie: { key, object } -> object\nremove = ?{ K key, O object } {\n .key str_to_bits index,\n .object list\n} remove_nth;\n\n-- Set string key in json? object: { key, val, json } -> json\nset = ?{ K key, json? val, json? json } {\n .key str_list? key,\n .val val,\n .json /object object\n} put |object json?;\n\n-- Remove string key from json? object: { key, json } -> json\ndel = ?{ K key, json? json } {\n .key str_list? key,\n .json /object object\n} remove |object json?;\n\n-- Set element at bits? index in json? list: { index, val, json } -> json\nset_nth = ?{ K index, json? val, json? json } {\n .index bits? index,\n .val val,\n .json /list list\n} put_nth |list json?;\n\n-- Remove element at bits? index from json? list: { index, json } -> json\ndel_nth = ?{ K index, json? json } {\n .index bits? index,\n .json /list list\n} remove_nth |list json?;\n\n---------------------------------------------------------------\n-- §6. Cardinality & Length Relations\n---------------------------------------------------------------\n\n_inv = ?{ < {} _, B 0, B 1 >=B x, B r } <\n { .x /_ if, .r then } .then,\n { .x /0 x, .r |0 r } _inv,\n { .x /1 x, .r |1 r } _inv\n> bits?;\n_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <\n { .x /_ if, .y |1 then } .then,\n { .x /0 x, .y |1 r } _inv,\n { .x /1 x, .y |0 y } _inc\n> bits?;\ninc_nat = { { () x, 0b r } _inv x, 0b y } _inc;\n\n_trie_size = ?{\n < {} nil, { < {} nil, V val > val, T 0, T 1 } trie >=T t,\n < {} _, B 0, B 1 >=B acc\n} <\n { .t /nil if, .acc then } .then,\n {\n .t /trie .val /nil if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc acc } _trie_size acc\n } _trie_size then\n } .then,\n {\n .t /trie .val /val if,\n {\n .t /trie .1 t,\n { .t /trie .0 t, .acc inc_nat acc } _trie_size acc\n } _trie_size then\n } .then\n> bits?;\n\nlist_size = { trie? t, 0 acc } _trie_size;\nobj_size = { trie? t, 0 acc } _trie_size;\n\n_str_len = ?{ string[] str, bits[] acc } <\n { .str /nil if, .acc then } .then,\n { .str /cons .cdr str, .acc inc_nat acc } _str_len\n> ? bits[];\nstr_len = { str_list? str, 0 acc } _str_len;\n\n-- Count items in json? (object entries, list elements, or string chars)\nlength_nat = json? <\n /object obj_size,\n /list list_size,\n /string str_len\n>;\n\npos_sign = {} |\"+\";\nbit0 = {} |0;\nbit1 = {} |1;\n\npos_zero = {\n pos_sign sign,\n {\n bit0 0, bit0 1, bit0 2, bit0 3, bit0 4,\n bit0 5, bit0 6, bit0 7, bit0 8, bit0 9, bit0 10\n } exponent,\n {\n bit0 0, bit0 1, bit0 2, bit0 3, bit0 4,\n bit0 5, bit0 6, bit0 7, bit0 8, bit0 9,\n bit0 10, bit0 11, bit0 12, bit0 13, bit0 14,\n bit0 15, bit0 16, bit0 17, bit0 18, bit0 19,\n bit0 20, bit0 21, bit0 22, bit0 23, bit0 24,\n bit0 25, bit0 26, bit0 27, bit0 28, bit0 29,\n bit0 30, bit0 31, bit0 32, bit0 33, bit0 34,\n bit0 35, bit0 36, bit0 37, bit0 38, bit0 39,\n bit0 40, bit0 41, bit0 42, bit0 43, bit0 44,\n bit0 45, bit0 46, bit0 47, bit0 48, bit0 49,\n bit0 50, bit0 51\n } fraction\n};\n\nexp_bias = {\n bit1 0, bit1 1, bit1 2, bit1 3, bit1 4,\n bit1 5, bit1 6, bit1 7, bit1 8, bit1 9, bit0 10\n};\n\nexp_half_adder = <\n { .a /0 a, .c_in /0 c_in } { bit0 sum, bit0 carry },\n { .a /0 a, .c_in /1 c_in } { bit1 sum, bit0 carry },\n { .a /1 a, .c_in /0 c_in } { bit1 sum, bit0 carry },\n { .a /1 a, .c_in /1 c_in } { bit0 sum, bit1 carry }\n>;\n\ninc_exp = {\n { .0 a, bit1 c_in } exp_half_adder e0,\n .1 b1, .2 b2, .3 b3, .4 b4, .5 b5, .6 b6, .7 b7, .8 b8, .9 b9, .10 b10\n} {\n .e0.sum 0,\n { .b1 a, .e0.carry c_in } exp_half_adder e1,\n .b2 b2, .b3 b3, .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .e1.sum 1,\n { .b2 a, .e1.carry c_in } exp_half_adder e2,\n .b3 b3, .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .e2.sum 2,\n { .b3 a, .e2.carry c_in } exp_half_adder e3,\n .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .e3.sum 3,\n { .b4 a, .e3.carry c_in } exp_half_adder e4,\n .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .e4.sum 4,\n { .b5 a, .e4.carry c_in } exp_half_adder e5,\n .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .e5.sum 5,\n { .b6 a, .e5.carry c_in } exp_half_adder e6,\n .b7 b7, .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .e6.sum 6,\n { .b7 a, .e6.carry c_in } exp_half_adder e7,\n .b8 b8, .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .e7.sum 7,\n { .b8 a, .e7.carry c_in } exp_half_adder e8,\n .b9 b9, .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .e8.sum 8,\n { .b9 a, .e8.carry c_in } exp_half_adder e9,\n .b10 b10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .8 8, .e9.sum 9,\n { .b10 a, .e9.carry c_in } exp_half_adder e10\n} {\n .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .8 8, .9 9, .e10.sum 10\n};\n\nshift_frac = ?{ bit[] b, fraction[] f } {\n .f.1 0, .f.2 1, .f.3 2, .f.4 3, .f.5 4,\n .f.6 5, .f.7 6, .f.8 7, .f.9 8, .f.10 9,\n .f.11 10, .f.12 11, .f.13 12, .f.14 13, .f.15 14,\n .f.16 15, .f.17 16, .f.18 17, .f.19 18, .f.20 19,\n .f.21 20, .f.22 21, .f.23 22, .f.24 23, .f.25 24,\n .f.26 25, .f.27 26, .f.28 27, .f.29 28, .f.30 29,\n .f.31 30, .f.32 31, .f.33 32, .f.34 33, .f.35 34,\n .f.36 35, .f.37 36, .f.38 37, .f.39 38, .f.40 39,\n .f.41 40, .f.42 41, .f.43 42, .f.44 43, .f.45 44,\n .f.46 45, .f.47 46, .f.48 47, .f.49 48, .f.50 49,\n .f.51 50, .b 51\n};\n\n_nat_step = ?{ bits[] bits, exponent[] exp, fraction[] frac } <\n {\n .bits /_ if,\n { pos_sign sign, .exp exponent, .frac fraction } then\n } .then,\n {\n .bits /0 bits,\n .exp inc_exp exp,\n { bit0 b, .frac f } shift_frac frac\n } _nat_step,\n {\n .bits /1 bits,\n .exp inc_exp exp,\n { bit1 b, .frac f } shift_frac frac\n } _nat_step\n>;\n\ninv_bits = { () x, 0b r } _inv;\n\nnat_to_float64 = bits? <\n /_ { pos_zero then } .then,\n /0 { pos_zero then } .then,\n /1 { () inv_bits bits, exp_bias exp, pos_zero .fraction frac } _nat_step\n>;\n\n-- Count items in json? -> json? number\nlength = length_nat nat_to_float64 |number json?;\n\n-- Append element to json? list: { val, json } -> json\npush = ?{ json? val, json? json } {\n (.json /list list_size) index,\n .val val,\n .json json\n} set_nth;\n\n---------------------------------------------------------------\n-- §7. Keys & Values Extraction\n---------------------------------------------------------------\n\nread_bit = bits? <\n /0 { {} |0 bit, () rest },\n /1 { {} |1 bit, () rest }\n>;\n\nread_bits7 =\n read_bit\n { .bit b6, (.rest read_bit) r5 }\n { .b6 b6, .r5 .bit b5, (.r5 .rest read_bit) r4 }\n { .b6 b6, .b5 b5, .r4 .bit b4, (.r4 .rest read_bit) r3 }\n { .b6 b6, .b5 b5, .b4 b4, .r3 .bit b3, (.r3 .rest read_bit) r2 }\n { .b6 b6, .b5 b5, .b4 b4, .b3 b3, .r2 .bit b2, (.r2 .rest read_bit) r1 }\n {\n .b6 b6, .b5 b5, .b4 b4, .b3 b3, .b2 b2,\n .r1 .bit b1, (.r1 .rest read_bit) r0\n }\n {\n { .r0 .bit 0, .b1 1, .b2 2, .b3 3, .b4 4, .b5 5, .b6 6 } |ascii val,\n .r0 .rest rest\n };\n\nread_char = bits? <\n /0 read_bits7\n>;\n\nbits_to_str = bits? <\n /_ {} |nil,\n read_char { .val car, (.rest bits_to_str) cdr } |cons\n> str_list?;\n\n_collect_keys = ?{\n btrie[V] tree,\n bits[] rev_path,\n list[string[]] acc\n} <\n { .tree /nil if, .acc then } .then,\n {\n .tree /trie .val /nil if,\n {\n .tree /trie .0 tree,\n .rev_path |0 rev_path,\n {\n .tree /trie .1 tree,\n .rev_path |1 rev_path,\n .acc acc\n } _collect_keys acc\n } _collect_keys then\n } .then,\n {\n .tree /trie .val /val if,\n {\n .tree /trie .0 tree,\n .rev_path |0 rev_path,\n {\n .tree /trie .1 tree,\n .rev_path |1 rev_path,\n {\n (.rev_path inv_bits bits_to_str) car,\n .acc cdr\n } |cons acc\n } _collect_keys acc\n } _collect_keys then\n } .then\n> ? list[string[]];\n\n_obj_keys_raw = { () tree, 0b rev_path, {} |nil acc } _collect_keys;\n\n-- Extract all keys from json? object as raw list: json? -> list[string[]]\nkeys_raw = json? /object _obj_keys_raw;\n\n_keys_to_trie = ?{\n list[string[]] klist,\n bits[] index,\n btrie[djson_btrie[]] acc\n} <\n { .klist /nil if, .acc then } .then,\n {\n .klist /cons .cdr klist,\n .index inc_nat index,\n {\n .index index,\n .klist /cons .car |string val,\n .acc list\n } put_nth acc\n } _keys_to_trie\n>;\n\n-- Extract all keys from json? object as a json? list: json? -> json?\nkeys = json? /object _obj_keys_raw\n { () klist, 0 index, {} |nil acc } _keys_to_trie\n |list json?;\n\n_collect_values = ?{\n btrie[V] tree,\n list[V] acc\n} <\n { .tree /nil if, .acc then } .then,\n {\n .tree /trie .val /nil if,\n {\n .tree /trie .0 tree,\n { .tree /trie .1 tree, .acc acc } _collect_values acc\n } _collect_values then\n } .then,\n {\n .tree /trie .val /val if,\n {\n .tree /trie .0 tree,\n {\n .tree /trie .1 tree,\n { .tree /trie .val /val car, .acc cdr } |cons acc\n } _collect_values acc\n } _collect_values then\n } .then\n> ? list[V];\n\n_obj_values_raw = { () tree, {} |nil acc } _collect_values;\n\n-- Extract all values from json? object or list as raw list\nvalues_raw = json? <\n /object _obj_values_raw,\n /list _obj_values_raw\n>;\n\n_values_to_trie = ?{\n list[djson_btrie[]] klist,\n bits[] index,\n btrie[djson_btrie[]] acc\n} <\n { .klist /nil if, .acc then } .then,\n {\n .klist /cons .cdr klist,\n .index inc_nat index,\n {\n .index index,\n .klist /cons .car val,\n .acc list\n } put_nth acc\n } _values_to_trie\n>;\n\n-- Extract all values from json? as a json? list: json? -> json?\nvalues = <\n json? /object _obj_values_raw\n { () klist, 0 index, {} |nil acc } _values_to_trie\n |list json?,\n json? /list |list json?\n>;\n\n---------------------------------------------------------------\n-- §8. Type Predicates\n---------------------------------------------------------------\n\nis_null? = json? /null ();\nis_bool? = json? /bool ();\nis_number? = json? /number ();\nis_string? = json? /string ();\nis_list? = json? /list ();\nis_object? = json? /object ();\n\njson_type[] = <\n {} null, {} bool, {} number, {} string, {} list, {} object\n>;\ntype_of = json? <\n /null {} |null,\n /bool {} |bool,\n /number {} |number,\n /string {} |string,\n /list {} |list,\n /object {} |object\n> ? json_type[];\n",
19
15
  "json.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT);\nvar BIT1 = Value.variant(\"1\", UNIT);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/string-codec.mjs\nvar STRING_PATTERN_PROPERTY_LIST = patternFromFilter(`\n? <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6\n }=Bits7 ascii,\n {\n <\n { Bit 0, Bit 1, Bit 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n { Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7 }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit 0, Bit 1, Bit 2, Bit 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit 0, Bit 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n> = Str\n`);\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT2 = Value.product({});\nfunction bitValue2(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT2);\n}\nfunction bitsProduct2(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue2(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT2),\n exponent: bitsProduct2(11, exponent),\n fraction: bitsProduct2(52, fraction)\n });\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant2(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct2(value, \"float64\");\n const sign = requireVariant2(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct2(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct2(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/json-codec.mjs\nvar UNIT3 = Value.product({});\nvar BOOL_PATTERN = patternFromFilter(\"?< {} false, {} true >\");\nvar NULL_PATTERN = patternFromFilter(\"?< {} null >\");\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(value ? \"true\" : \"false\", UNIT3);\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return encodeNumberToValue(value);\n }\n if (typeof value === \"string\") {\n return textToStringValue(value);\n }\n if (Array.isArray(value)) {\n return Value.product(\n value.reduce((product, item, index) => {\n product[String(index)] = fromJsonValue(item);\n return product;\n }, {})\n );\n }\n if (value && typeof value === \"object\") {\n return Value.product(\n Object.keys(value).reduce((product, key) => {\n product[key] = fromJsonValue(value[key]);\n return product;\n }, {})\n );\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction composePattern(kind, entries) {\n const result = [[kind, []]];\n for (const [label, childPattern] of entries) {\n const offset = result.length;\n result[0][1].push([label, offset]);\n for (const [childKind, childEdges] of childPattern) {\n result.push([\n childKind,\n childEdges.map(([edgeLabel, target]) => [edgeLabel, target + offset])\n ]);\n }\n }\n return result;\n}\nfunction patternFromJsonValue(value) {\n if (value === null) return NULL_PATTERN;\n if (typeof value === \"boolean\") return BOOL_PATTERN;\n if (typeof value === \"number\") return FLOAT64_PATTERN;\n if (typeof value === \"string\") return STRING_PATTERN_PROPERTY_LIST;\n if (Array.isArray(value)) {\n return composePattern(\n \"closed-product\",\n value.map((item, index) => [String(index), patternFromJsonValue(item)])\n );\n }\n if (value && typeof value === \"object\") {\n return composePattern(\n \"closed-product\",\n Object.keys(value).sort((a, b) => a < b ? -1 : a > b ? 1 : 0).map((key) => [key, patternFromJsonValue(value[key])])\n );\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (isVariant(value)) {\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"json.null\");\n return null;\n }\n if (value.tag === \"true\") {\n requireProduct3(value.value, \"json.true\");\n return true;\n }\n if (value.tag === \"false\") {\n requireProduct3(value.value, \"json.false\");\n return false;\n }\n return stringValueToText(value);\n }\n const product = requireProduct3(value, \"json.value\");\n const keys = Object.keys(product);\n const isArray = keys.every((key, index) => key === String(index));\n if (isArray) {\n return keys.map((key) => {\n const child = product[key];\n if (isVariant(child)) return toJsonValue(child);\n if (isProduct(child)) {\n try {\n return decodeValueToNumber(child);\n } catch {\n }\n }\n return toJsonValue(child);\n });\n }\n const numberKeys = [\"exponent\", \"fraction\", \"sign\"];\n if (keys.length === numberKeys.length && numberKeys.every((key) => key in product)) {\n return decodeValueToNumber(value);\n }\n try {\n return stringValueToText(value);\n } catch {\n }\n return keys.reduce((result, key) => {\n result[key] = toJsonValue(product[key]);\n return result;\n }, {});\n}\n\n// codecs/json.mjs\nvar doc2 = `\nSchema-specializing JSON codec (RFC 8259).\n\nTranslates JSON data to/from self-describing enveloped k values where\neach JSON document derives a document-specific pattern:\n - objects map to products { field: value, ... }\n - arrays map to indexed products { \"0\": a0, \"1\": a1, ... }\n - numbers, strings, booleans, and null map to canonical k values\n(See djson.mjs for the universal singleton-pattern dynamic JSON codec.)\n`;\nfunction parse2(text) {\n const json = JSON.parse(text);\n const value = fromJsonValue(json);\n const pattern = patternFromJsonValue(json);\n return withPattern(value, pattern);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n doc2 as doc,\n parse2 as parse,\n print2 as print\n};\n",
20
- "djson.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT = Value.product({});\nfunction bitValue(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT);\n}\nfunction bitsProduct(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT),\n exponent: bitsProduct(11, exponent),\n fraction: bitsProduct(52, fraction)\n });\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct(value, \"float64\");\n const sign = requireVariant(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT2 = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT2);\nvar BIT1 = Value.variant(\"1\", UNIT2);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue2(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct2(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue2(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant2(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct2(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct2(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct2(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct2(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct2(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct2(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT2);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct2(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct2(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct2(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant2(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct2(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct2(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct2(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct2(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct2(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct2(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct2(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct2(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT2);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant2(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct2(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct2(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct2(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct2(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct2(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT2);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant2(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct2(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct2(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct2(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct2(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant2(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct2(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct2(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct2(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct2(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct2(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct2(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT2);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant2(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct2(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct2(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/djson.mjs\nvar doc2 = `\nDynamic JSON (djson) codec with universal singleton pattern.\n\nTranslates any JSON value to/from self-describing enveloped k values\nconforming to the universal recursive JSON AST type:\n JSON = <\n float64 number,\n str string,\n <{} true, {} false> bool,\n {} null,\n <{} nil, {JSON car, LIST cdr} cons> = LIST list,\n <{} nil, {{str key, JSON val} car, OBJ cdr} cons> = OBJ object\n >\n`;\nvar DJSON_PATTERN = patternFromFilter(`\n? <\n {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10, Bit 11,\n Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17,\n Bit 18, Bit 19, Bit 20, Bit 21, Bit 22, Bit 23,\n Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35,\n Bit 36, Bit 37, Bit 38, Bit 39, Bit 40, Bit 41,\n Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47,\n Bit 48, Bit 49, Bit 50, Bit 51\n } fraction\n } number,\n\n <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit7 0,\n Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5, Bit7 6\n }=Bits7 ascii,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit7 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3,\n Bit7 4, Bit7 5, Bit7 6, Bit7 7\n }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2, Bit7 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5,\n Bit7 6, Bit7 7, Bit7 8, Bit7 9, Bit7 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit7 0, Bit7 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n > = Str string,\n\n < {} true, {} false > bool,\n {} null,\n\n < {} nil, { JSON car, LIST cdr } cons > = LIST list,\n\n < {} nil, { { Str key, JSON val } car, OBJ cdr } cons > = OBJ object\n> = JSON\n`);\nvar UNIT3 = Value.product({});\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction listToK(items) {\n let result = Value.variant(\"nil\", UNIT3);\n for (let i = items.length - 1; i >= 0; i--) {\n result = Value.variant(\"cons\", Value.product({\n car: items[i],\n cdr: result\n }));\n }\n return result;\n}\nfunction listFromK(value, where) {\n const result = [];\n let current = value;\n while (true) {\n if (!isVariant(current)) {\n throw new Error(`${where}: expected list variant (nil or cons)`);\n }\n if (current.tag === \"nil\") {\n requireProduct3(current.value, `${where}.nil`);\n return result;\n }\n if (current.tag !== \"cons\") {\n throw new Error(\n `${where}: expected nil or cons, got ${current.tag}`\n );\n }\n const pair = requireProduct3(current.value, `${where}.cons`);\n result.push(pair.car);\n current = pair.cdr;\n }\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(\n \"bool\",\n Value.variant(value ? \"true\" : \"false\", UNIT3)\n );\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return Value.variant(\"number\", encodeNumberToValue(value));\n }\n if (typeof value === \"string\") {\n return Value.variant(\"string\", textToStringValue(value));\n }\n if (Array.isArray(value)) {\n return Value.variant(\"list\", listToK(value.map(fromJsonValue)));\n }\n if (value && typeof value === \"object\") {\n const keys = Object.keys(value).sort();\n const entries = keys.map(\n (k) => Value.product({\n key: textToStringValue(k),\n val: fromJsonValue(value[k])\n })\n );\n return Value.variant(\"object\", listToK(entries));\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (!isVariant(value)) {\n throw new Error(\"djson.value: expected top-level Variant\");\n }\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"djson.null\");\n return null;\n }\n if (value.tag === \"bool\") {\n if (!isVariant(value.value)) {\n throw new Error(\"djson.bool: expected variant\");\n }\n if (value.value.tag === \"true\") {\n requireProduct3(value.value.value, \"djson.bool.true\");\n return true;\n }\n if (value.value.tag === \"false\") {\n requireProduct3(value.value.value, \"djson.bool.false\");\n return false;\n }\n throw new Error(`djson.bool: invalid tag ${value.value.tag}`);\n }\n if (value.tag === \"number\") {\n return decodeValueToNumber(value.value);\n }\n if (value.tag === \"string\") {\n return stringValueToText(value.value);\n }\n if (value.tag === \"list\") {\n const items = listFromK(value.value, \"djson.list\");\n return items.map(toJsonValue);\n }\n if (value.tag === \"object\") {\n const pairs = listFromK(value.value, \"djson.object\");\n const result = {};\n for (const pairValue of pairs) {\n const pair = requireProduct3(pairValue, \"djson.object.pair\");\n const key = stringValueToText(pair.key);\n result[key] = toJsonValue(pair.val);\n }\n return result;\n }\n throw new Error(`Unsupported djson tag: ${value.tag}`);\n}\nfunction parse2(text) {\n const json = JSON.parse(text);\n const value = fromJsonValue(json);\n return withPattern(value, DJSON_PATTERN);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n DJSON_PATTERN,\n doc2 as doc,\n fromJsonValue,\n parse2 as parse,\n print2 as print,\n toJsonValue\n};\n",
21
- "djson-btrie.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern2) {\n if (!pattern2) return value;\n if (value.pattern === pattern2) return value;\n if (isProduct(value)) return Value.product(value.product, pattern2);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern2);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern2 = null) {\n return new _Value({ type: \"{}\", pattern: pattern2, product });\n }\n static variant(tag, value, pattern2 = null) {\n return new _Value({ type: \"<>\", pattern: pattern2, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT = Value.product({});\nfunction bitValue(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT);\n}\nfunction bitsProduct(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT),\n exponent: bitsProduct(11, exponent),\n fraction: bitsProduct(52, fraction)\n });\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct(value, \"float64\");\n const sign = requireVariant(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT2 = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT2);\nvar BIT1 = Value.variant(\"1\", UNIT2);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue2(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct2(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue2(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant2(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct2(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct2(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct2(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct2(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct2(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct2(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT2);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct2(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct2(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct2(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant2(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct2(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct2(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct2(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct2(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct2(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct2(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct2(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct2(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT2);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant2(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct2(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct2(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct2(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct2(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct2(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT2);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant2(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct2(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct2(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct2(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct2(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant2(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct2(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct2(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct2(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct2(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct2(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct2(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT2);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant2(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct2(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct2(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/djson-btrie.mjs\nvar doc2 = `\nDynamic JSON Binary Bit-Trie (djson-btrie) codec.\n\nRepresents both JSON arrays (list) and JSON objects (object) using a\nunified binary bit-trie:\n TRIE = <\n {} nil,\n {\n < {} nil, JSON val > val,\n TRIE 0,\n TRIE 1\n } trie\n >\n`;\nvar DJSON_BTRIE_PATTERN = patternFromFilter(`\n? <\n {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10, Bit 11,\n Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17,\n Bit 18, Bit 19, Bit 20, Bit 21, Bit 22, Bit 23,\n Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35,\n Bit 36, Bit 37, Bit 38, Bit 39, Bit 40, Bit 41,\n Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47,\n Bit 48, Bit 49, Bit 50, Bit 51\n } fraction\n } number,\n\n <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit7 0,\n Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5, Bit7 6\n }=Bits7 ascii,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit7 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3,\n Bit7 4, Bit7 5, Bit7 6, Bit7 7\n }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2, Bit7 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5,\n Bit7 6, Bit7 7, Bit7 8, Bit7 9, Bit7 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit7 0, Bit7 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n > = Str string,\n\n < {} true, {} false > bool,\n {} null,\n\n <\n {} nil,\n {\n < {} nil, JSON val > val,\n TRIE 0,\n TRIE 1\n } trie\n > = TRIE list,\n\n TRIE object\n> = JSON\n`);\nvar UNIT3 = Value.product({});\nvar NIL = Value.variant(\"nil\", UNIT3);\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction pushBitsMSB(out, width, n) {\n for (let i = width - 1; i >= 0; i--) {\n out.push(n >> i & 1);\n }\n}\nfunction readBitsMSB(bits, offset, width) {\n let n = 0;\n for (let i = 0; i < width; i++) {\n n = n << 1 | bits[offset + i];\n }\n return n;\n}\nfunction indexToBitPath(index) {\n if (index === 0) return [0];\n const bits = [];\n let n = index;\n while (n > 0) {\n bits.push(n & 1);\n n = Math.floor(n / 2);\n }\n bits.reverse();\n return bits;\n}\nfunction bitPathToIndex(bits) {\n if (bits.length === 1 && bits[0] === 0) return 0;\n let n = 0;\n for (const b of bits) {\n n = n * 2 + b;\n }\n return n;\n}\nfunction codePointToBitPath(cp, out = []) {\n if (cp <= 127) {\n out.push(0);\n pushBitsMSB(out, 7, cp);\n return out;\n }\n if (cp <= 2047) {\n out.push(1, 0);\n pushBitsMSB(out, 11, cp);\n return out;\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n out.push(1, 1, 1, 0);\n if (hi <= 239) {\n out.push(0);\n pushBitsMSB(out, 4, hi - 224);\n } else if (hi <= 247) {\n out.push(1, 0);\n pushBitsMSB(out, 3, hi - 240);\n } else {\n out.push(1, 1);\n }\n pushBitsMSB(out, 8, lo2);\n return out;\n }\n out.push(1, 1, 0);\n if (hi >= 8 && hi <= 15) {\n out.push(0, 0, 0);\n pushBitsMSB(out, 3, hi - 8);\n } else if (hi >= 16 && hi <= 127) {\n out.push(0, 0, 1);\n pushBitsMSB(out, 7, hi - 16);\n } else if (hi >= 128 && hi <= 207) {\n out.push(0, 1, 0);\n pushBitsMSB(out, 7, hi - 128);\n } else if (hi >= 208 && hi <= 215) {\n out.push(0, 1, 1);\n pushBitsMSB(out, 3, hi - 208);\n } else if (hi === 249) {\n out.push(1, 0, 0);\n } else if (hi >= 250 && hi <= 251) {\n out.push(1, 0, 1);\n pushBitsMSB(out, 1, hi - 250);\n } else if (hi >= 252 && hi <= 253) {\n out.push(1, 1, 0);\n pushBitsMSB(out, 1, hi - 252);\n } else if (hi >= 254 && hi <= 255) {\n out.push(1, 1, 1);\n pushBitsMSB(out, 1, hi - 254);\n } else {\n throw new Error(`Unsupported BMP hi byte: ${hi}`);\n }\n pushBitsMSB(out, 8, lo2);\n return out;\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n out.push(1, 1, 1, 1, 0);\n pushBitsMSB(out, 8, mid);\n pushBitsMSB(out, 8, lo);\n return out;\n }\n const plane = cp >> 16;\n out.push(1, 1, 1, 1, 1);\n if (plane >= 2 && plane <= 3) {\n out.push(0, 0);\n pushBitsMSB(out, 1, plane - 2);\n } else if (plane >= 4 && plane <= 7) {\n out.push(0, 1);\n pushBitsMSB(out, 2, plane - 4);\n } else if (plane >= 8 && plane <= 15) {\n out.push(1, 0);\n pushBitsMSB(out, 3, plane - 8);\n } else if (plane === 16) {\n out.push(1, 1);\n } else {\n throw new Error(`Unsupported plane: ${plane}`);\n }\n pushBitsMSB(out, 8, mid);\n pushBitsMSB(out, 8, lo);\n return out;\n}\nfunction stringToBitPath(str) {\n const out = [];\n for (const ch of str) {\n codePointToBitPath(ch.codePointAt(0), out);\n }\n return out;\n}\nfunction bitPathToString(bits) {\n let i = 0;\n let out = \"\";\n while (i < bits.length) {\n if (bits[i] === 0) {\n const cp = readBitsMSB(bits, i + 1, 7);\n out += String.fromCodePoint(cp);\n i += 8;\n continue;\n }\n if (bits[i + 1] === 0) {\n const cp = readBitsMSB(bits, i + 2, 11);\n out += String.fromCodePoint(cp);\n i += 13;\n continue;\n }\n if (bits[i + 2] === 0) {\n i += 3;\n const tag = readBitsMSB(bits, i, 3);\n i += 3;\n let hi;\n if (tag === 0) {\n hi = 8 + readBitsMSB(bits, i, 3);\n i += 3;\n } else if (tag === 1) {\n hi = 16 + readBitsMSB(bits, i, 7);\n i += 7;\n } else if (tag === 2) {\n hi = 128 + readBitsMSB(bits, i, 7);\n i += 7;\n } else if (tag === 3) {\n hi = 208 + readBitsMSB(bits, i, 3);\n i += 3;\n } else if (tag === 4) {\n hi = 249;\n } else if (tag === 5) {\n hi = 250 + readBitsMSB(bits, i, 1);\n i += 1;\n } else if (tag === 6) {\n hi = 252 + readBitsMSB(bits, i, 1);\n i += 1;\n } else {\n hi = 254 + readBitsMSB(bits, i, 1);\n i += 1;\n }\n const lo2 = readBitsMSB(bits, i, 8);\n i += 8;\n out += String.fromCodePoint(hi << 8 | lo2);\n continue;\n }\n if (bits[i + 3] === 0) {\n i += 4;\n let hi;\n if (bits[i] === 0) {\n hi = 224 + readBitsMSB(bits, i + 1, 4);\n i += 5;\n } else if (bits[i + 1] === 0) {\n hi = 240 + readBitsMSB(bits, i + 2, 3);\n i += 5;\n } else {\n hi = 248;\n i += 2;\n }\n const lo2 = readBitsMSB(bits, i, 8);\n i += 8;\n out += String.fromCodePoint(hi << 8 | lo2);\n continue;\n }\n if (bits[i + 4] === 0) {\n i += 5;\n const mid2 = readBitsMSB(bits, i, 8);\n const lo2 = readBitsMSB(bits, i + 8, 8);\n i += 16;\n out += String.fromCodePoint(1 << 16 | mid2 << 8 | lo2);\n continue;\n }\n i += 5;\n let plane;\n if (bits[i] === 0 && bits[i + 1] === 0) {\n plane = 2 + readBitsMSB(bits, i + 2, 1);\n i += 3;\n } else if (bits[i] === 0 && bits[i + 1] === 1) {\n plane = 4 + readBitsMSB(bits, i + 2, 2);\n i += 4;\n } else if (bits[i] === 1 && bits[i + 1] === 0) {\n plane = 8 + readBitsMSB(bits, i + 2, 3);\n i += 5;\n } else {\n plane = 16;\n i += 2;\n }\n const mid = readBitsMSB(bits, i, 8);\n const lo = readBitsMSB(bits, i + 8, 8);\n i += 16;\n out += String.fromCodePoint(plane << 16 | mid << 8 | lo);\n }\n return out;\n}\nfunction makeMutableNode() {\n return { val: null, b0: null, b1: null };\n}\nfunction insertMutableTrie(root, bits, kVal) {\n let curr = root;\n for (let i = 0; i < bits.length; i++) {\n if (bits[i] === 0) {\n if (!curr.b0) curr.b0 = makeMutableNode();\n curr = curr.b0;\n } else {\n if (!curr.b1) curr.b1 = makeMutableNode();\n curr = curr.b1;\n }\n }\n curr.val = kVal;\n}\nfunction mutableNodeToK(node) {\n if (!node) return NIL;\n const valField = node.val !== null ? Value.variant(\"val\", node.val) : NIL;\n const b0 = mutableNodeToK(node.b0);\n const b1 = mutableNodeToK(node.b1);\n if (node.val === null && b0 === NIL && b1 === NIL) {\n return NIL;\n }\n return Value.variant(\"trie\", Value.product({\n val: valField,\n 0: b0,\n 1: b1\n }));\n}\nfunction collectTrieEntries(kNode, pathBits = [], entries = []) {\n if (!isVariant(kNode)) {\n throw new Error(\"djson-btrie: expected variant (nil or trie)\");\n }\n if (kNode.tag === \"nil\") {\n return entries;\n }\n if (kNode.tag !== \"trie\") {\n throw new Error(`djson-btrie: unexpected tag ${kNode.tag}`);\n }\n const prod = requireProduct3(kNode.value, \"djson-btrie.trie\");\n if (!isVariant(prod.val)) {\n throw new Error(\"djson-btrie.trie.val: expected variant\");\n }\n if (prod.val.tag === \"val\") {\n entries.push({ bits: pathBits.slice(), val: prod.val.value });\n } else if (prod.val.tag !== \"nil\") {\n throw new Error(\n `djson-btrie.trie.val: unexpected tag ${prod.val.tag}`\n );\n }\n pathBits.push(0);\n collectTrieEntries(prod[0], pathBits, entries);\n pathBits.pop();\n pathBits.push(1);\n collectTrieEntries(prod[1], pathBits, entries);\n pathBits.pop();\n return entries;\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(\n \"bool\",\n Value.variant(value ? \"true\" : \"false\", UNIT3)\n );\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return Value.variant(\"number\", encodeNumberToValue(value));\n }\n if (typeof value === \"string\") {\n return Value.variant(\"string\", textToStringValue(value));\n }\n if (Array.isArray(value)) {\n if (value.length === 0) {\n return Value.variant(\"list\", NIL);\n }\n const root = makeMutableNode();\n for (let i = 0; i < value.length; i++) {\n insertMutableTrie(root, indexToBitPath(i), fromJsonValue(value[i]));\n }\n return Value.variant(\"list\", mutableNodeToK(root));\n }\n if (value && typeof value === \"object\") {\n const keys = Object.keys(value);\n if (keys.length === 0) {\n return Value.variant(\"object\", NIL);\n }\n const root = makeMutableNode();\n for (const k of keys) {\n insertMutableTrie(\n root,\n stringToBitPath(k),\n fromJsonValue(value[k])\n );\n }\n return Value.variant(\"object\", mutableNodeToK(root));\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (!isVariant(value)) {\n throw new Error(\"djson-btrie: expected top-level Variant\");\n }\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"djson-btrie.null\");\n return null;\n }\n if (value.tag === \"bool\") {\n if (!isVariant(value.value)) {\n throw new Error(\"djson-btrie.bool: expected variant\");\n }\n if (value.value.tag === \"true\") return true;\n if (value.value.tag === \"false\") return false;\n throw new Error(`djson-btrie.bool: invalid tag ${value.value.tag}`);\n }\n if (value.tag === \"number\") {\n return decodeValueToNumber(value.value);\n }\n if (value.tag === \"string\") {\n return stringValueToText(value.value);\n }\n if (value.tag === \"list\") {\n const entries = collectTrieEntries(value.value);\n entries.sort(\n (a, b) => bitPathToIndex(a.bits) - bitPathToIndex(b.bits)\n );\n return entries.map((e) => toJsonValue(e.val));\n }\n if (value.tag === \"object\") {\n const entries = collectTrieEntries(value.value);\n const result = {};\n for (const e of entries) {\n result[bitPathToString(e.bits)] = toJsonValue(e.val);\n }\n return result;\n }\n throw new Error(`djson-btrie: unsupported tag ${value.tag}`);\n}\nvar pattern = DJSON_BTRIE_PATTERN;\nfunction parse2(rawText) {\n const text = String(rawText).trim();\n if (!text) throw new Error(\"Empty JSON input\");\n const parsed = JSON.parse(text);\n const val = fromJsonValue(parsed);\n return withPattern(val, DJSON_BTRIE_PATTERN);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nvar codec = {\n name: \"djson-btrie\",\n doc: doc2,\n pattern: DJSON_BTRIE_PATTERN,\n parse: parse2,\n print: print2\n};\nvar djson_btrie_default = codec;\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n DJSON_BTRIE_PATTERN,\n bitPathToIndex,\n bitPathToString,\n codePointToBitPath,\n codec,\n djson_btrie_default as default,\n doc2 as doc,\n fromJsonValue,\n indexToBitPath,\n parse2 as parse,\n pattern,\n print2 as print,\n stringToBitPath,\n toJsonValue\n};\n",
22
- "djson-ptrie.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern2) {\n if (!pattern2) return value;\n if (value.pattern === pattern2) return value;\n if (isProduct(value)) return Value.product(value.product, pattern2);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern2);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern2 = null) {\n return new _Value({ type: \"{}\", pattern: pattern2, product });\n }\n static variant(tag, value, pattern2 = null) {\n return new _Value({ type: \"<>\", pattern: pattern2, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT = Value.product({});\nfunction bitValue(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT);\n}\nfunction bitsProduct(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT),\n exponent: bitsProduct(11, exponent),\n fraction: bitsProduct(52, fraction)\n });\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct(value, \"float64\");\n const sign = requireVariant(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT2 = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT2);\nvar BIT1 = Value.variant(\"1\", UNIT2);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue2(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct2(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue2(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant2(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct2(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct2(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct2(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct2(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct2(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct2(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT2);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct2(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct2(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct2(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant2(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct2(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct2(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct2(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct2(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct2(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct2(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct2(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct2(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT2);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant2(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct2(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct2(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct2(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct2(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct2(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT2);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant2(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct2(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct2(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct2(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct2(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant2(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct2(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct2(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct2(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct2(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct2(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct2(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT2);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant2(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct2(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct2(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/djson-btrie.mjs\nvar doc2 = `\nDynamic JSON Binary Bit-Trie (djson-btrie) codec.\n\nRepresents both JSON arrays (list) and JSON objects (object) using a\nunified binary bit-trie:\n TRIE = <\n {} nil,\n {\n < {} nil, JSON val > val,\n TRIE 0,\n TRIE 1\n } trie\n >\n`;\nvar DJSON_BTRIE_PATTERN = patternFromFilter(`\n? <\n {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10, Bit 11,\n Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17,\n Bit 18, Bit 19, Bit 20, Bit 21, Bit 22, Bit 23,\n Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35,\n Bit 36, Bit 37, Bit 38, Bit 39, Bit 40, Bit 41,\n Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47,\n Bit 48, Bit 49, Bit 50, Bit 51\n } fraction\n } number,\n\n <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit7 0,\n Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5, Bit7 6\n }=Bits7 ascii,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit7 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3,\n Bit7 4, Bit7 5, Bit7 6, Bit7 7\n }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2, Bit7 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5,\n Bit7 6, Bit7 7, Bit7 8, Bit7 9, Bit7 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit7 0, Bit7 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n > = Str string,\n\n < {} true, {} false > bool,\n {} null,\n\n <\n {} nil,\n {\n < {} nil, JSON val > val,\n TRIE 0,\n TRIE 1\n } trie\n > = TRIE list,\n\n TRIE object\n> = JSON\n`);\nvar UNIT3 = Value.product({});\nvar NIL = Value.variant(\"nil\", UNIT3);\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction pushBitsMSB(out, width, n) {\n for (let i = width - 1; i >= 0; i--) {\n out.push(n >> i & 1);\n }\n}\nfunction readBitsMSB(bits, offset, width) {\n let n = 0;\n for (let i = 0; i < width; i++) {\n n = n << 1 | bits[offset + i];\n }\n return n;\n}\nfunction indexToBitPath(index) {\n if (index === 0) return [0];\n const bits = [];\n let n = index;\n while (n > 0) {\n bits.push(n & 1);\n n = Math.floor(n / 2);\n }\n bits.reverse();\n return bits;\n}\nfunction bitPathToIndex(bits) {\n if (bits.length === 1 && bits[0] === 0) return 0;\n let n = 0;\n for (const b of bits) {\n n = n * 2 + b;\n }\n return n;\n}\nfunction codePointToBitPath(cp, out = []) {\n if (cp <= 127) {\n out.push(0);\n pushBitsMSB(out, 7, cp);\n return out;\n }\n if (cp <= 2047) {\n out.push(1, 0);\n pushBitsMSB(out, 11, cp);\n return out;\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n out.push(1, 1, 1, 0);\n if (hi <= 239) {\n out.push(0);\n pushBitsMSB(out, 4, hi - 224);\n } else if (hi <= 247) {\n out.push(1, 0);\n pushBitsMSB(out, 3, hi - 240);\n } else {\n out.push(1, 1);\n }\n pushBitsMSB(out, 8, lo2);\n return out;\n }\n out.push(1, 1, 0);\n if (hi >= 8 && hi <= 15) {\n out.push(0, 0, 0);\n pushBitsMSB(out, 3, hi - 8);\n } else if (hi >= 16 && hi <= 127) {\n out.push(0, 0, 1);\n pushBitsMSB(out, 7, hi - 16);\n } else if (hi >= 128 && hi <= 207) {\n out.push(0, 1, 0);\n pushBitsMSB(out, 7, hi - 128);\n } else if (hi >= 208 && hi <= 215) {\n out.push(0, 1, 1);\n pushBitsMSB(out, 3, hi - 208);\n } else if (hi === 249) {\n out.push(1, 0, 0);\n } else if (hi >= 250 && hi <= 251) {\n out.push(1, 0, 1);\n pushBitsMSB(out, 1, hi - 250);\n } else if (hi >= 252 && hi <= 253) {\n out.push(1, 1, 0);\n pushBitsMSB(out, 1, hi - 252);\n } else if (hi >= 254 && hi <= 255) {\n out.push(1, 1, 1);\n pushBitsMSB(out, 1, hi - 254);\n } else {\n throw new Error(`Unsupported BMP hi byte: ${hi}`);\n }\n pushBitsMSB(out, 8, lo2);\n return out;\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n out.push(1, 1, 1, 1, 0);\n pushBitsMSB(out, 8, mid);\n pushBitsMSB(out, 8, lo);\n return out;\n }\n const plane = cp >> 16;\n out.push(1, 1, 1, 1, 1);\n if (plane >= 2 && plane <= 3) {\n out.push(0, 0);\n pushBitsMSB(out, 1, plane - 2);\n } else if (plane >= 4 && plane <= 7) {\n out.push(0, 1);\n pushBitsMSB(out, 2, plane - 4);\n } else if (plane >= 8 && plane <= 15) {\n out.push(1, 0);\n pushBitsMSB(out, 3, plane - 8);\n } else if (plane === 16) {\n out.push(1, 1);\n } else {\n throw new Error(`Unsupported plane: ${plane}`);\n }\n pushBitsMSB(out, 8, mid);\n pushBitsMSB(out, 8, lo);\n return out;\n}\nfunction stringToBitPath(str) {\n const out = [];\n for (const ch of str) {\n codePointToBitPath(ch.codePointAt(0), out);\n }\n return out;\n}\nfunction bitPathToString(bits) {\n let i = 0;\n let out = \"\";\n while (i < bits.length) {\n if (bits[i] === 0) {\n const cp = readBitsMSB(bits, i + 1, 7);\n out += String.fromCodePoint(cp);\n i += 8;\n continue;\n }\n if (bits[i + 1] === 0) {\n const cp = readBitsMSB(bits, i + 2, 11);\n out += String.fromCodePoint(cp);\n i += 13;\n continue;\n }\n if (bits[i + 2] === 0) {\n i += 3;\n const tag = readBitsMSB(bits, i, 3);\n i += 3;\n let hi;\n if (tag === 0) {\n hi = 8 + readBitsMSB(bits, i, 3);\n i += 3;\n } else if (tag === 1) {\n hi = 16 + readBitsMSB(bits, i, 7);\n i += 7;\n } else if (tag === 2) {\n hi = 128 + readBitsMSB(bits, i, 7);\n i += 7;\n } else if (tag === 3) {\n hi = 208 + readBitsMSB(bits, i, 3);\n i += 3;\n } else if (tag === 4) {\n hi = 249;\n } else if (tag === 5) {\n hi = 250 + readBitsMSB(bits, i, 1);\n i += 1;\n } else if (tag === 6) {\n hi = 252 + readBitsMSB(bits, i, 1);\n i += 1;\n } else {\n hi = 254 + readBitsMSB(bits, i, 1);\n i += 1;\n }\n const lo2 = readBitsMSB(bits, i, 8);\n i += 8;\n out += String.fromCodePoint(hi << 8 | lo2);\n continue;\n }\n if (bits[i + 3] === 0) {\n i += 4;\n let hi;\n if (bits[i] === 0) {\n hi = 224 + readBitsMSB(bits, i + 1, 4);\n i += 5;\n } else if (bits[i + 1] === 0) {\n hi = 240 + readBitsMSB(bits, i + 2, 3);\n i += 5;\n } else {\n hi = 248;\n i += 2;\n }\n const lo2 = readBitsMSB(bits, i, 8);\n i += 8;\n out += String.fromCodePoint(hi << 8 | lo2);\n continue;\n }\n if (bits[i + 4] === 0) {\n i += 5;\n const mid2 = readBitsMSB(bits, i, 8);\n const lo2 = readBitsMSB(bits, i + 8, 8);\n i += 16;\n out += String.fromCodePoint(1 << 16 | mid2 << 8 | lo2);\n continue;\n }\n i += 5;\n let plane;\n if (bits[i] === 0 && bits[i + 1] === 0) {\n plane = 2 + readBitsMSB(bits, i + 2, 1);\n i += 3;\n } else if (bits[i] === 0 && bits[i + 1] === 1) {\n plane = 4 + readBitsMSB(bits, i + 2, 2);\n i += 4;\n } else if (bits[i] === 1 && bits[i + 1] === 0) {\n plane = 8 + readBitsMSB(bits, i + 2, 3);\n i += 5;\n } else {\n plane = 16;\n i += 2;\n }\n const mid = readBitsMSB(bits, i, 8);\n const lo = readBitsMSB(bits, i + 8, 8);\n i += 16;\n out += String.fromCodePoint(plane << 16 | mid << 8 | lo);\n }\n return out;\n}\nfunction makeMutableNode() {\n return { val: null, b0: null, b1: null };\n}\nfunction insertMutableTrie(root, bits, kVal) {\n let curr = root;\n for (let i = 0; i < bits.length; i++) {\n if (bits[i] === 0) {\n if (!curr.b0) curr.b0 = makeMutableNode();\n curr = curr.b0;\n } else {\n if (!curr.b1) curr.b1 = makeMutableNode();\n curr = curr.b1;\n }\n }\n curr.val = kVal;\n}\nfunction mutableNodeToK(node) {\n if (!node) return NIL;\n const valField = node.val !== null ? Value.variant(\"val\", node.val) : NIL;\n const b0 = mutableNodeToK(node.b0);\n const b1 = mutableNodeToK(node.b1);\n if (node.val === null && b0 === NIL && b1 === NIL) {\n return NIL;\n }\n return Value.variant(\"trie\", Value.product({\n val: valField,\n 0: b0,\n 1: b1\n }));\n}\nfunction collectTrieEntries(kNode, pathBits = [], entries = []) {\n if (!isVariant(kNode)) {\n throw new Error(\"djson-btrie: expected variant (nil or trie)\");\n }\n if (kNode.tag === \"nil\") {\n return entries;\n }\n if (kNode.tag !== \"trie\") {\n throw new Error(`djson-btrie: unexpected tag ${kNode.tag}`);\n }\n const prod = requireProduct3(kNode.value, \"djson-btrie.trie\");\n if (!isVariant(prod.val)) {\n throw new Error(\"djson-btrie.trie.val: expected variant\");\n }\n if (prod.val.tag === \"val\") {\n entries.push({ bits: pathBits.slice(), val: prod.val.value });\n } else if (prod.val.tag !== \"nil\") {\n throw new Error(\n `djson-btrie.trie.val: unexpected tag ${prod.val.tag}`\n );\n }\n pathBits.push(0);\n collectTrieEntries(prod[0], pathBits, entries);\n pathBits.pop();\n pathBits.push(1);\n collectTrieEntries(prod[1], pathBits, entries);\n pathBits.pop();\n return entries;\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(\n \"bool\",\n Value.variant(value ? \"true\" : \"false\", UNIT3)\n );\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return Value.variant(\"number\", encodeNumberToValue(value));\n }\n if (typeof value === \"string\") {\n return Value.variant(\"string\", textToStringValue(value));\n }\n if (Array.isArray(value)) {\n if (value.length === 0) {\n return Value.variant(\"list\", NIL);\n }\n const root = makeMutableNode();\n for (let i = 0; i < value.length; i++) {\n insertMutableTrie(root, indexToBitPath(i), fromJsonValue(value[i]));\n }\n return Value.variant(\"list\", mutableNodeToK(root));\n }\n if (value && typeof value === \"object\") {\n const keys = Object.keys(value);\n if (keys.length === 0) {\n return Value.variant(\"object\", NIL);\n }\n const root = makeMutableNode();\n for (const k of keys) {\n insertMutableTrie(\n root,\n stringToBitPath(k),\n fromJsonValue(value[k])\n );\n }\n return Value.variant(\"object\", mutableNodeToK(root));\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (!isVariant(value)) {\n throw new Error(\"djson-btrie: expected top-level Variant\");\n }\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"djson-btrie.null\");\n return null;\n }\n if (value.tag === \"bool\") {\n if (!isVariant(value.value)) {\n throw new Error(\"djson-btrie.bool: expected variant\");\n }\n if (value.value.tag === \"true\") return true;\n if (value.value.tag === \"false\") return false;\n throw new Error(`djson-btrie.bool: invalid tag ${value.value.tag}`);\n }\n if (value.tag === \"number\") {\n return decodeValueToNumber(value.value);\n }\n if (value.tag === \"string\") {\n return stringValueToText(value.value);\n }\n if (value.tag === \"list\") {\n const entries = collectTrieEntries(value.value);\n entries.sort(\n (a, b) => bitPathToIndex(a.bits) - bitPathToIndex(b.bits)\n );\n return entries.map((e) => toJsonValue(e.val));\n }\n if (value.tag === \"object\") {\n const entries = collectTrieEntries(value.value);\n const result = {};\n for (const e of entries) {\n result[bitPathToString(e.bits)] = toJsonValue(e.val);\n }\n return result;\n }\n throw new Error(`djson-btrie: unsupported tag ${value.tag}`);\n}\nfunction parse2(rawText) {\n const text = String(rawText).trim();\n if (!text) throw new Error(\"Empty JSON input\");\n const parsed = JSON.parse(text);\n const val = fromJsonValue(parsed);\n return withPattern(val, DJSON_BTRIE_PATTERN);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\n\n// codecs/djson-ptrie.mjs\nvar doc3 = `\nDynamic JSON Patricia/Radix Trie (djson-ptrie) codec (trie-array style).\n\nRepresents JSON arrays with a compressed bit-radix trie (with prefix &\nsize) and JSON objects with a character-level radix trie (with prefix,\nsize, val, and sorted dict).\n`;\nvar DJSON_PTRIE_PATTERN = patternFromFilter(`\n? <\n {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10, Bit 11,\n Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17,\n Bit 18, Bit 19, Bit 20, Bit 21, Bit 22, Bit 23,\n Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35,\n Bit 36, Bit 37, Bit 38, Bit 39, Bit 40, Bit 41,\n Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47,\n Bit 48, Bit 49, Bit 50, Bit 51\n } fraction\n } number,\n\n <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit7 0,\n Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5, Bit7 6\n }=Bits7 ascii,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit7 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3,\n Bit7 4, Bit7 5, Bit7 6, Bit7 7\n }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2, Bit7 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5,\n Bit7 6, Bit7 7, Bit7 8, Bit7 9, Bit7 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit7 0, Bit7 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n >=CHAR car,\n Str cdr\n } cons\n > = Str string,\n\n < {} true, {} false > bool,\n {} null,\n\n <\n {} nil,\n {\n < {} _, BITS 0, BITS 1 >=BITS prefix,\n BITS size,\n < {} nil, JSON val > val,\n LIST 0,\n LIST 1\n } trie\n > = LIST list,\n\n <\n {} nil,\n {\n Str prefix,\n BITS size,\n < {} nil, JSON val > val,\n <\n {} nil,\n {\n { CHAR char, OBJ trie } car,\n DICT cdr\n } cons\n >=DICT dict\n } trie\n > = OBJ object\n> = JSON\n`);\nvar UNIT4 = Value.product({});\nvar NIL2 = Value.variant(\"nil\", UNIT4);\nvar BITS_EMPTY = Value.variant(\"_\", UNIT4);\nfunction requireProduct4(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction bitArrayToBitsK(bits) {\n let out = BITS_EMPTY;\n for (let i = bits.length - 1; i >= 0; i--) {\n out = Value.variant(bits[i] === 1 ? \"1\" : \"0\", out);\n }\n return out;\n}\nfunction bitsKToBitArray(val) {\n const out = [];\n let cur = val;\n while (true) {\n if (!isVariant(cur)) {\n throw new Error(\"djson-ptrie.bits: expected variant\");\n }\n if (cur.tag === \"_\") return out;\n if (cur.tag === \"0\") {\n out.push(0);\n cur = cur.value;\n } else if (cur.tag === \"1\") {\n out.push(1);\n cur = cur.value;\n } else {\n throw new Error(`djson-ptrie.bits: unexpected tag ${cur.tag}`);\n }\n }\n}\nfunction natToBitsK(n) {\n return bitArrayToBitsK(indexToBitPath(n));\n}\nfunction commonBitPrefixLen(a, b) {\n const len = Math.min(a.length, b.length);\n let i = 0;\n while (i < len && a[i] === b[i]) i++;\n return i;\n}\nfunction splitBitTrieNode(x, n) {\n const bitAtN = x.prefix[n];\n const y = {\n size: x.size - 1,\n prefix: x.prefix.slice(n + 1),\n val: x.val,\n b0: x.b0,\n b1: x.b1\n };\n x.prefix = x.prefix.slice(0, n);\n x.val = null;\n x.b0 = bitAtN === 0 ? y : null;\n x.b1 = bitAtN === 1 ? y : null;\n}\nfunction addToBitTrie(t, bits, kVal) {\n let curr = t;\n let s = bits;\n while (true) {\n curr.size++;\n const pLen = commonBitPrefixLen(curr.prefix, s);\n if (pLen === s.length && pLen === curr.prefix.length) {\n curr.val = kVal;\n return;\n }\n if (pLen === s.length) {\n splitBitTrieNode(curr, pLen);\n curr.val = kVal;\n return;\n }\n if (pLen !== curr.prefix.length) {\n splitBitTrieNode(curr, pLen);\n const nextBit2 = s[pLen];\n const newChild = {\n size: 1,\n prefix: s.slice(pLen + 1),\n val: kVal,\n b0: null,\n b1: null\n };\n if (nextBit2 === 0) curr.b0 = newChild;\n else curr.b1 = newChild;\n return;\n }\n const nextBit = s[pLen];\n const q = nextBit === 0 ? curr.b0 : curr.b1;\n if (!q) {\n const newChild = {\n size: 1,\n prefix: s.slice(pLen + 1),\n val: kVal,\n b0: null,\n b1: null\n };\n if (nextBit === 0) curr.b0 = newChild;\n else curr.b1 = newChild;\n return;\n }\n curr = q;\n s = s.slice(pLen + 1);\n }\n}\nfunction bitTrieNodeToK(node) {\n if (!node) return NIL2;\n return Value.variant(\"trie\", Value.product({\n prefix: bitArrayToBitsK(node.prefix),\n size: natToBitsK(node.size),\n val: node.val !== null ? Value.variant(\"val\", node.val) : NIL2,\n 0: bitTrieNodeToK(node.b0),\n 1: bitTrieNodeToK(node.b1)\n }));\n}\nfunction collectBitTrieEntries(kNode, pathBits = [], entries = []) {\n if (!isVariant(kNode)) {\n throw new Error(\"djson-ptrie.list: expected variant\");\n }\n if (kNode.tag === \"nil\") return entries;\n if (kNode.tag !== \"trie\") {\n throw new Error(`djson-ptrie.list: unexpected tag ${kNode.tag}`);\n }\n const prod = requireProduct4(kNode.value, \"djson-ptrie.list.trie\");\n const nodePrefix = bitsKToBitArray(prod.prefix);\n const fullPrefix = pathBits.concat(nodePrefix);\n if (isVariant(prod.val) && prod.val.tag === \"val\") {\n entries.push({ bits: fullPrefix, val: prod.val.value });\n }\n collectBitTrieEntries(prod[0], fullPrefix.concat([0]), entries);\n collectBitTrieEntries(prod[1], fullPrefix.concat([1]), entries);\n return entries;\n}\nfunction commonCharPrefixLen(a, b) {\n const len = Math.min(a.length, b.length);\n let i = 0;\n while (i < len && a[i] === b[i]) i++;\n return i;\n}\nfunction splitCharTrieNode(x, n) {\n const charAtN = x.prefix[n];\n const y = {\n size: x.size - 1,\n prefix: x.prefix.slice(n + 1),\n val: x.val,\n dict: x.dict,\n order: x.order\n };\n x.dict = /* @__PURE__ */ new Map([[charAtN, y]]);\n x.order = [charAtN];\n x.prefix = x.prefix.slice(0, n);\n x.val = null;\n}\nfunction addToCharTrie(t, cpArray, kVal) {\n let curr = t;\n let s = cpArray;\n while (true) {\n curr.size++;\n const pLen = commonCharPrefixLen(curr.prefix, s);\n if (pLen === s.length && pLen === curr.prefix.length) {\n curr.val = kVal;\n return;\n }\n if (pLen === s.length) {\n splitCharTrieNode(curr, pLen);\n curr.val = kVal;\n return;\n }\n if (pLen !== curr.prefix.length) {\n splitCharTrieNode(curr, pLen);\n const c2 = s[pLen];\n curr.dict.set(c2, {\n size: 1,\n prefix: s.slice(pLen + 1),\n val: kVal,\n dict: /* @__PURE__ */ new Map(),\n order: []\n });\n curr.order.push(c2);\n curr.order.sort((a, b) => a - b);\n return;\n }\n const c = s[pLen];\n const q = curr.dict.get(c);\n if (!q) {\n curr.dict.set(c, {\n size: 1,\n prefix: s.slice(pLen + 1),\n val: kVal,\n dict: /* @__PURE__ */ new Map(),\n order: []\n });\n curr.order.push(c);\n curr.order.sort((a, b) => a - b);\n return;\n }\n curr = q;\n s = s.slice(pLen + 1);\n }\n}\nfunction cpArrayToStringK(cpArray) {\n let out = NIL2;\n for (let i = cpArray.length - 1; i >= 0; i--) {\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cpArray[i]),\n cdr: out\n }));\n }\n return out;\n}\nfunction charTrieNodeToK(node) {\n if (!node) return NIL2;\n let dictK = NIL2;\n for (let i = node.order.length - 1; i >= 0; i--) {\n const cp = node.order[i];\n const child = node.dict.get(cp);\n dictK = Value.variant(\"cons\", Value.product({\n car: Value.product({\n char: codePointToUnicodeValue(cp),\n trie: charTrieNodeToK(child)\n }),\n cdr: dictK\n }));\n }\n return Value.variant(\"trie\", Value.product({\n prefix: cpArrayToStringK(node.prefix),\n size: natToBitsK(node.size),\n val: node.val !== null ? Value.variant(\"val\", node.val) : NIL2,\n dict: dictK\n }));\n}\nfunction collectCharTrieEntries(kNode, prefixStr = \"\", entries = []) {\n if (!isVariant(kNode)) {\n throw new Error(\"djson-ptrie.object: expected variant\");\n }\n if (kNode.tag === \"nil\") return entries;\n if (kNode.tag !== \"trie\") {\n throw new Error(`djson-ptrie.object: unexpected tag ${kNode.tag}`);\n }\n const prod = requireProduct4(kNode.value, \"djson-ptrie.object.trie\");\n const nodePrefix = stringValueToText(prod.prefix);\n const fullPrefix = prefixStr + nodePrefix;\n if (isVariant(prod.val) && prod.val.tag === \"val\") {\n entries.push({ key: fullPrefix, val: prod.val.value });\n }\n let curDict = prod.dict;\n while (true) {\n if (!isVariant(curDict)) {\n throw new Error(\"djson-ptrie.object.dict: expected variant\");\n }\n if (curDict.tag === \"nil\") break;\n if (curDict.tag !== \"cons\") {\n throw new Error(\n `djson-ptrie.object.dict: unexpected tag ${curDict.tag}`\n );\n }\n const pair = requireProduct4(\n curDict.value,\n \"djson-ptrie.object.dict.cons\"\n );\n const entry = requireProduct4(pair.car, \"djson-ptrie.object.dict.car\");\n const ch = String.fromCodePoint(unicodeValueToCodePoint(entry.char));\n collectCharTrieEntries(entry.trie, fullPrefix + ch, entries);\n curDict = pair.cdr;\n }\n return entries;\n}\nfunction fromJsonValue2(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT4);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(\n \"bool\",\n Value.variant(value ? \"true\" : \"false\", UNIT4)\n );\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return Value.variant(\"number\", encodeNumberToValue(value));\n }\n if (typeof value === \"string\") {\n return Value.variant(\"string\", textToStringValue(value));\n }\n if (Array.isArray(value)) {\n if (value.length === 0) {\n return Value.variant(\"list\", NIL2);\n }\n const firstBits = indexToBitPath(0);\n const root = {\n size: 1,\n prefix: firstBits,\n val: fromJsonValue2(value[0]),\n b0: null,\n b1: null\n };\n for (let i = 1; i < value.length; i++) {\n addToBitTrie(root, indexToBitPath(i), fromJsonValue2(value[i]));\n }\n return Value.variant(\"list\", bitTrieNodeToK(root));\n }\n if (value && typeof value === \"object\") {\n const keys = Object.keys(value);\n if (keys.length === 0) {\n return Value.variant(\"object\", NIL2);\n }\n const firstCps = Array.from(keys[0]).map((c) => c.codePointAt(0));\n const root = {\n size: 1,\n prefix: firstCps,\n val: fromJsonValue2(value[keys[0]]),\n dict: /* @__PURE__ */ new Map(),\n order: []\n };\n for (let i = 1; i < keys.length; i++) {\n const cps = Array.from(keys[i]).map((c) => c.codePointAt(0));\n addToCharTrie(root, cps, fromJsonValue2(value[keys[i]]));\n }\n return Value.variant(\"object\", charTrieNodeToK(root));\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue2(value) {\n if (!isVariant(value)) {\n throw new Error(\"djson-ptrie: expected top-level Variant\");\n }\n if (value.tag === \"null\") {\n requireProduct4(value.value, \"djson-ptrie.null\");\n return null;\n }\n if (value.tag === \"bool\") {\n if (!isVariant(value.value)) {\n throw new Error(\"djson-ptrie.bool: expected variant\");\n }\n if (value.value.tag === \"true\") return true;\n if (value.value.tag === \"false\") return false;\n throw new Error(`djson-ptrie.bool: invalid tag ${value.value.tag}`);\n }\n if (value.tag === \"number\") {\n return decodeValueToNumber(value.value);\n }\n if (value.tag === \"string\") {\n return stringValueToText(value.value);\n }\n if (value.tag === \"list\") {\n const entries = collectBitTrieEntries(value.value);\n entries.sort(\n (a, b) => bitPathToIndex(a.bits) - bitPathToIndex(b.bits)\n );\n return entries.map((e) => toJsonValue2(e.val));\n }\n if (value.tag === \"object\") {\n const entries = collectCharTrieEntries(value.value);\n const result = {};\n for (const e of entries) {\n result[e.key] = toJsonValue2(e.val);\n }\n return result;\n }\n throw new Error(`djson-ptrie: unsupported tag ${value.tag}`);\n}\nvar pattern = DJSON_PTRIE_PATTERN;\nfunction parse3(rawText) {\n const text = String(rawText).trim();\n if (!text) throw new Error(\"Empty JSON input\");\n const parsed = JSON.parse(text);\n const val = fromJsonValue2(parsed);\n return withPattern(val, DJSON_PTRIE_PATTERN);\n}\nfunction print3(value) {\n return JSON.stringify(toJsonValue2(value));\n}\nvar codec = {\n name: \"djson-ptrie\",\n doc: doc3,\n pattern: DJSON_PTRIE_PATTERN,\n parse: parse3,\n print: print3\n};\nvar djson_ptrie_default = codec;\nrunCodecCLI(import.meta.url, { parse: parse3, print: print3, doc: doc3 });\nexport {\n DJSON_PTRIE_PATTERN,\n codec,\n djson_ptrie_default as default,\n doc3 as doc,\n fromJsonValue2 as fromJsonValue,\n parse3 as parse,\n pattern,\n print3 as print,\n toJsonValue2 as toJsonValue\n};\n",
16
+ "djson.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern2) {\n if (!pattern2) return value;\n if (value.pattern === pattern2) return value;\n if (isProduct(value)) return Value.product(value.product, pattern2);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern2);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern2 = null) {\n return new _Value({ type: \"{}\", pattern: pattern2, product });\n }\n static variant(tag, value, pattern2 = null) {\n return new _Value({ type: \"<>\", pattern: pattern2, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT = Value.product({});\nfunction bitValue(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT);\n}\nfunction bitsProduct(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT),\n exponent: bitsProduct(11, exponent),\n fraction: bitsProduct(52, fraction)\n });\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct(value, \"float64\");\n const sign = requireVariant(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT2 = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT2);\nvar BIT1 = Value.variant(\"1\", UNIT2);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue2(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct2(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue2(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant2(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct2(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct2(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct2(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct2(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct2(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct2(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT2);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct2(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct2(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct2(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant2(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct2(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct2(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct2(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct2(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct2(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct2(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct2(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct2(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT2);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant2(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct2(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct2(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct2(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct2(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct2(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT2);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant2(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct2(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct2(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct2(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct2(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant2(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct2(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct2(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct2(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct2(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct2(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct2(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT2);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant2(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct2(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct2(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/djson.mjs\nvar doc2 = `\nDynamic JSON (djson) binary bit-trie codec.\n\nRepresents both JSON arrays (list) and JSON objects (object) using a\nunified binary bit-trie:\n TRIE = <\n {} nil,\n {\n < {} nil, JSON val > val,\n TRIE 0,\n TRIE 1\n } trie\n >\n`;\nvar DJSON_PATTERN = patternFromFilter(`\n? <\n {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10, Bit 11,\n Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17,\n Bit 18, Bit 19, Bit 20, Bit 21, Bit 22, Bit 23,\n Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35,\n Bit 36, Bit 37, Bit 38, Bit 39, Bit 40, Bit 41,\n Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47,\n Bit 48, Bit 49, Bit 50, Bit 51\n } fraction\n } number,\n\n <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit7 0,\n Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5, Bit7 6\n }=Bits7 ascii,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit7 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3,\n Bit7 4, Bit7 5, Bit7 6, Bit7 7\n }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2, Bit7 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5,\n Bit7 6, Bit7 7, Bit7 8, Bit7 9, Bit7 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit7 0, Bit7 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n > = Str string,\n\n < {} true, {} false > bool,\n {} null,\n\n <\n {} nil,\n {\n < {} nil, JSON val > val,\n TRIE 0,\n TRIE 1\n } trie\n > = TRIE list,\n\n TRIE object\n> = JSON\n`);\nvar UNIT3 = Value.product({});\nvar NIL = Value.variant(\"nil\", UNIT3);\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction pushBitsMSB(out, width, n) {\n for (let i = width - 1; i >= 0; i--) {\n out.push(n >> i & 1);\n }\n}\nfunction readBitsMSB(bits, offset, width) {\n let n = 0;\n for (let i = 0; i < width; i++) {\n n = n << 1 | bits[offset + i];\n }\n return n;\n}\nfunction indexToBitPath(index) {\n if (index === 0) return [0];\n const bits = [];\n let n = index;\n while (n > 0) {\n bits.push(n & 1);\n n = Math.floor(n / 2);\n }\n bits.reverse();\n return bits;\n}\nfunction bitPathToIndex(bits) {\n if (bits.length === 1 && bits[0] === 0) return 0;\n let n = 0;\n for (const b of bits) {\n n = n * 2 + b;\n }\n return n;\n}\nfunction codePointToBitPath(cp, out = []) {\n if (cp <= 127) {\n out.push(0);\n pushBitsMSB(out, 7, cp);\n return out;\n }\n if (cp <= 2047) {\n out.push(1, 0);\n pushBitsMSB(out, 11, cp);\n return out;\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n out.push(1, 1, 1, 0);\n if (hi <= 239) {\n out.push(0);\n pushBitsMSB(out, 4, hi - 224);\n } else if (hi <= 247) {\n out.push(1, 0);\n pushBitsMSB(out, 3, hi - 240);\n } else {\n out.push(1, 1);\n }\n pushBitsMSB(out, 8, lo2);\n return out;\n }\n out.push(1, 1, 0);\n if (hi >= 8 && hi <= 15) {\n out.push(0, 0, 0);\n pushBitsMSB(out, 3, hi - 8);\n } else if (hi >= 16 && hi <= 127) {\n out.push(0, 0, 1);\n pushBitsMSB(out, 7, hi - 16);\n } else if (hi >= 128 && hi <= 207) {\n out.push(0, 1, 0);\n pushBitsMSB(out, 7, hi - 128);\n } else if (hi >= 208 && hi <= 215) {\n out.push(0, 1, 1);\n pushBitsMSB(out, 3, hi - 208);\n } else if (hi === 249) {\n out.push(1, 0, 0);\n } else if (hi >= 250 && hi <= 251) {\n out.push(1, 0, 1);\n pushBitsMSB(out, 1, hi - 250);\n } else if (hi >= 252 && hi <= 253) {\n out.push(1, 1, 0);\n pushBitsMSB(out, 1, hi - 252);\n } else if (hi >= 254 && hi <= 255) {\n out.push(1, 1, 1);\n pushBitsMSB(out, 1, hi - 254);\n } else {\n throw new Error(`Unsupported BMP hi byte: ${hi}`);\n }\n pushBitsMSB(out, 8, lo2);\n return out;\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n out.push(1, 1, 1, 1, 0);\n pushBitsMSB(out, 8, mid);\n pushBitsMSB(out, 8, lo);\n return out;\n }\n const plane = cp >> 16;\n out.push(1, 1, 1, 1, 1);\n if (plane >= 2 && plane <= 3) {\n out.push(0, 0);\n pushBitsMSB(out, 1, plane - 2);\n } else if (plane >= 4 && plane <= 7) {\n out.push(0, 1);\n pushBitsMSB(out, 2, plane - 4);\n } else if (plane >= 8 && plane <= 15) {\n out.push(1, 0);\n pushBitsMSB(out, 3, plane - 8);\n } else if (plane === 16) {\n out.push(1, 1);\n } else {\n throw new Error(`Unsupported plane: ${plane}`);\n }\n pushBitsMSB(out, 8, mid);\n pushBitsMSB(out, 8, lo);\n return out;\n}\nfunction stringToBitPath(str) {\n const out = [];\n for (const ch of str) {\n codePointToBitPath(ch.codePointAt(0), out);\n }\n return out;\n}\nfunction bitPathToString(bits) {\n let i = 0;\n let out = \"\";\n while (i < bits.length) {\n if (bits[i] === 0) {\n const cp = readBitsMSB(bits, i + 1, 7);\n out += String.fromCodePoint(cp);\n i += 8;\n continue;\n }\n if (bits[i + 1] === 0) {\n const cp = readBitsMSB(bits, i + 2, 11);\n out += String.fromCodePoint(cp);\n i += 13;\n continue;\n }\n if (bits[i + 2] === 0) {\n i += 3;\n const tag = readBitsMSB(bits, i, 3);\n i += 3;\n let hi;\n if (tag === 0) {\n hi = 8 + readBitsMSB(bits, i, 3);\n i += 3;\n } else if (tag === 1) {\n hi = 16 + readBitsMSB(bits, i, 7);\n i += 7;\n } else if (tag === 2) {\n hi = 128 + readBitsMSB(bits, i, 7);\n i += 7;\n } else if (tag === 3) {\n hi = 208 + readBitsMSB(bits, i, 3);\n i += 3;\n } else if (tag === 4) {\n hi = 249;\n } else if (tag === 5) {\n hi = 250 + readBitsMSB(bits, i, 1);\n i += 1;\n } else if (tag === 6) {\n hi = 252 + readBitsMSB(bits, i, 1);\n i += 1;\n } else {\n hi = 254 + readBitsMSB(bits, i, 1);\n i += 1;\n }\n const lo2 = readBitsMSB(bits, i, 8);\n i += 8;\n out += String.fromCodePoint(hi << 8 | lo2);\n continue;\n }\n if (bits[i + 3] === 0) {\n i += 4;\n let hi;\n if (bits[i] === 0) {\n hi = 224 + readBitsMSB(bits, i + 1, 4);\n i += 5;\n } else if (bits[i + 1] === 0) {\n hi = 240 + readBitsMSB(bits, i + 2, 3);\n i += 5;\n } else {\n hi = 248;\n i += 2;\n }\n const lo2 = readBitsMSB(bits, i, 8);\n i += 8;\n out += String.fromCodePoint(hi << 8 | lo2);\n continue;\n }\n if (bits[i + 4] === 0) {\n i += 5;\n const mid2 = readBitsMSB(bits, i, 8);\n const lo2 = readBitsMSB(bits, i + 8, 8);\n i += 16;\n out += String.fromCodePoint(1 << 16 | mid2 << 8 | lo2);\n continue;\n }\n i += 5;\n let plane;\n if (bits[i] === 0 && bits[i + 1] === 0) {\n plane = 2 + readBitsMSB(bits, i + 2, 1);\n i += 3;\n } else if (bits[i] === 0 && bits[i + 1] === 1) {\n plane = 4 + readBitsMSB(bits, i + 2, 2);\n i += 4;\n } else if (bits[i] === 1 && bits[i + 1] === 0) {\n plane = 8 + readBitsMSB(bits, i + 2, 3);\n i += 5;\n } else {\n plane = 16;\n i += 2;\n }\n const mid = readBitsMSB(bits, i, 8);\n const lo = readBitsMSB(bits, i + 8, 8);\n i += 16;\n out += String.fromCodePoint(plane << 16 | mid << 8 | lo);\n }\n return out;\n}\nfunction makeMutableNode() {\n return { val: null, b0: null, b1: null };\n}\nfunction insertMutableTrie(root, bits, kVal) {\n let curr = root;\n for (let i = 0; i < bits.length; i++) {\n if (bits[i] === 0) {\n if (!curr.b0) curr.b0 = makeMutableNode();\n curr = curr.b0;\n } else {\n if (!curr.b1) curr.b1 = makeMutableNode();\n curr = curr.b1;\n }\n }\n curr.val = kVal;\n}\nfunction mutableNodeToK(node) {\n if (!node) return NIL;\n const valField = node.val !== null ? Value.variant(\"val\", node.val) : NIL;\n const b0 = mutableNodeToK(node.b0);\n const b1 = mutableNodeToK(node.b1);\n if (node.val === null && b0 === NIL && b1 === NIL) {\n return NIL;\n }\n return Value.variant(\"trie\", Value.product({\n val: valField,\n 0: b0,\n 1: b1\n }));\n}\nfunction collectTrieEntries(kNode, pathBits = [], entries = []) {\n if (!isVariant(kNode)) {\n throw new Error(\"djson: expected variant (nil or trie)\");\n }\n if (kNode.tag === \"nil\") {\n return entries;\n }\n if (kNode.tag !== \"trie\") {\n throw new Error(`djson: unexpected tag ${kNode.tag}`);\n }\n const prod = requireProduct3(kNode.value, \"djson.trie\");\n if (!isVariant(prod.val)) {\n throw new Error(\"djson.trie.val: expected variant\");\n }\n if (prod.val.tag === \"val\") {\n entries.push({ bits: pathBits.slice(), val: prod.val.value });\n } else if (prod.val.tag !== \"nil\") {\n throw new Error(\n `djson.trie.val: unexpected tag ${prod.val.tag}`\n );\n }\n pathBits.push(0);\n collectTrieEntries(prod[0], pathBits, entries);\n pathBits.pop();\n pathBits.push(1);\n collectTrieEntries(prod[1], pathBits, entries);\n pathBits.pop();\n return entries;\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(\n \"bool\",\n Value.variant(value ? \"true\" : \"false\", UNIT3)\n );\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return Value.variant(\"number\", encodeNumberToValue(value));\n }\n if (typeof value === \"string\") {\n return Value.variant(\"string\", textToStringValue(value));\n }\n if (Array.isArray(value)) {\n if (value.length === 0) {\n return Value.variant(\"list\", NIL);\n }\n const root = makeMutableNode();\n for (let i = 0; i < value.length; i++) {\n insertMutableTrie(root, indexToBitPath(i), fromJsonValue(value[i]));\n }\n return Value.variant(\"list\", mutableNodeToK(root));\n }\n if (value && typeof value === \"object\") {\n const keys = Object.keys(value);\n if (keys.length === 0) {\n return Value.variant(\"object\", NIL);\n }\n const root = makeMutableNode();\n for (const k of keys) {\n insertMutableTrie(\n root,\n stringToBitPath(k),\n fromJsonValue(value[k])\n );\n }\n return Value.variant(\"object\", mutableNodeToK(root));\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (!isVariant(value)) {\n throw new Error(\"djson: expected top-level Variant\");\n }\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"djson.null\");\n return null;\n }\n if (value.tag === \"bool\") {\n if (!isVariant(value.value)) {\n throw new Error(\"djson.bool: expected variant\");\n }\n if (value.value.tag === \"true\") return true;\n if (value.value.tag === \"false\") return false;\n throw new Error(`djson.bool: invalid tag ${value.value.tag}`);\n }\n if (value.tag === \"number\") {\n return decodeValueToNumber(value.value);\n }\n if (value.tag === \"string\") {\n return stringValueToText(value.value);\n }\n if (value.tag === \"list\") {\n const entries = collectTrieEntries(value.value);\n entries.sort(\n (a, b) => bitPathToIndex(a.bits) - bitPathToIndex(b.bits)\n );\n return entries.map((e) => toJsonValue(e.val));\n }\n if (value.tag === \"object\") {\n const entries = collectTrieEntries(value.value);\n const result = {};\n for (const e of entries) {\n result[bitPathToString(e.bits)] = toJsonValue(e.val);\n }\n return result;\n }\n throw new Error(`djson: unsupported tag ${value.tag}`);\n}\nvar DJSON_BTRIE_PATTERN = DJSON_PATTERN;\nvar pattern = DJSON_PATTERN;\nfunction parse2(rawText) {\n const text = String(rawText).trim();\n if (!text) throw new Error(\"Empty JSON input\");\n const parsed = JSON.parse(text);\n const val = fromJsonValue(parsed);\n return withPattern(val, DJSON_PATTERN);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nvar codec = {\n name: \"djson\",\n doc: doc2,\n pattern: DJSON_PATTERN,\n parse: parse2,\n print: print2\n};\nvar djson_default = codec;\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n DJSON_BTRIE_PATTERN,\n DJSON_PATTERN,\n bitPathToIndex,\n bitPathToString,\n codePointToBitPath,\n codec,\n djson_default as default,\n doc2 as doc,\n fromJsonValue,\n indexToBitPath,\n parse2 as parse,\n pattern,\n print2 as print,\n stringToBitPath,\n toJsonValue\n};\n",
23
17
  "utf8.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT);\nvar BIT1 = Value.variant(\"1\", UNIT);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/string-codec.mjs\nvar STRING_PATTERN_PROPERTY_LIST = patternFromFilter(`\n? <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6\n }=Bits7 ascii,\n {\n <\n { Bit 0, Bit 1, Bit 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n { Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7 }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit 0, Bit 1, Bit 2, Bit 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit 0, Bit 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n> = Str\n`);\n\n// codecs/utf8.mjs\nvar doc = `\nUTF-8 text codec.\n\nEncodes/decodes text strings to/from the canonical k string pattern.\n`;\nfunction parse(text) {\n const value = textToStringValue(text);\n return withPattern(value, STRING_PATTERN_PROPERTY_LIST);\n}\nfunction print(value) {\n return stringValueToText(value);\n}\nrunCodecCLI(import.meta.url, { parse, print, doc, addNewline: false });\nexport {\n doc,\n parse,\n print\n};\n",
24
18
  "int.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/int.mjs\nvar doc = `\nDecimal integers and integer lists.\n\nSyntax:\n Decimal integer (e.g. 42, -15) or bracketed integer list\n (e.g. [0, 1, 2], []).\n`;\nvar INT_PATTERN = patternFromFilter('?< <bits 0, bits 1, {} _>=bits \"+\", bits \"-\">');\nvar INT_LIST_PATTERN = patternFromFilter(`\n ?<\n {} nil,\n {\n < <bits 0, bits 1, {} _>=bits \"+\", bits \"-\" > car,\n list cdr\n } cons\n > = list\n`);\nfunction buildBits(n) {\n const bitChars = n === 0n ? [\"0\"] : n.toString(2).split(\"\");\n let v = Value.variant(\"_\", Value.product({}));\n for (let i = bitChars.length - 1; i >= 0; i--) {\n v = Value.variant(bitChars[i], v);\n }\n return v;\n}\nfunction parseIntStr(str) {\n str = str.trim();\n let sign = \"+\";\n if (str[0] === \"-\") {\n sign = \"-\";\n str = str.slice(1).replace(/\\s+/g, \"\");\n } else if (str[0] === \"+\") {\n str = str.slice(1).replace(/\\s+/g, \"\");\n }\n if (!/^(0|[1-9][0-9]*)$/.test(str)) {\n throw new Error(`Invalid integer syntax: ${JSON.stringify(str)}`);\n }\n const n = BigInt(str);\n if (n === 0n) sign = \"+\";\n return Value.variant(sign, buildBits(n));\n}\nfunction parseIntListStr(str) {\n str = str.trim();\n if (!str.startsWith(\"[\") || !str.endsWith(\"]\")) {\n throw new Error(`Invalid integer list syntax: ${JSON.stringify(str)}`);\n }\n const inner = str.slice(1, -1).trim();\n if (inner === \"\") {\n return Value.variant(\"nil\", Value.product({}));\n }\n const parts = inner.split(\",\");\n const items = parts.map((part) => parseIntStr(part));\n let result = Value.variant(\"nil\", Value.product({}));\n for (let i = items.length - 1; i >= 0; i--) {\n result = Value.variant(\"cons\", Value.product({\n car: items[i],\n cdr: result\n }));\n }\n return result;\n}\nfunction parse(str) {\n str = str.trim();\n const raw = str.startsWith(\"[\") ? parseIntListStr(str) : parseIntStr(str);\n const pattern = isVariant(raw) && (raw.tag === \"nil\" || raw.tag === \"cons\") ? INT_LIST_PATTERN : INT_PATTERN;\n return withPattern(raw, pattern);\n}\nfunction printSingleInt(value) {\n if (!isVariant(value) || value.tag !== \"+\" && value.tag !== \"-\") {\n throw new Error(\"Not a valid k int value\");\n }\n const sign = value.tag;\n let bits = \"\";\n let node = value.value;\n while (isVariant(node) && node.tag !== \"_\") {\n bits += node.tag;\n node = node.value;\n }\n const n = bits === \"\" ? 0n : BigInt(\"0b\" + bits);\n const digits = n.toString(10);\n if (n === 0n) return \"0\";\n return (sign === \"-\" ? \"-\" : \"\") + digits;\n}\nfunction printIntList(value) {\n const items = [];\n let node = value;\n while (isVariant(node) && node.tag === \"cons\") {\n const product = isProduct(node.value) ? node.value.product : null;\n if (!product || !product.car || !product.cdr) {\n throw new Error(\"Invalid list cons node\");\n }\n items.push(printSingleInt(product.car));\n node = product.cdr;\n }\n if (!isVariant(node) || node.tag !== \"nil\") {\n throw new Error(\"Not a valid k int list: does not terminate in nil\");\n }\n return `[${items.join(\",\")}]`;\n}\nfunction print(value) {\n if (isVariant(value)) {\n if (value.tag === \"nil\" || value.tag === \"cons\") {\n return printIntList(value);\n }\n if (value.tag === \"+\" || value.tag === \"-\") {\n return printSingleInt(value);\n }\n }\n throw new Error(\"Not a valid k int or int list value\");\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\nexport {\n INT_LIST_PATTERN,\n INT_PATTERN,\n doc,\n parse,\n parseIntListStr,\n parseIntStr,\n print\n};\n",
25
19
  "unit.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/unit.mjs\nvar doc = `\nUnit codec for {}.\n\nTranslates the empty product {} to/from binary pattern+value stream.\n`;\nvar UNIT_PATTERN = patternFromFilter(\"?{}\");\nfunction parse() {\n return withPattern(Value.product({}), UNIT_PATTERN);\n}\nfunction print(value) {\n if (!isProduct(value) || Object.keys(value.product).length !== 0) {\n throw new Error(\"Input is not a unit value\");\n }\n return \"{}\";\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\nexport {\n UNIT_PATTERN,\n doc,\n parse,\n print\n};\n",