~/bend-docscommunity

Json.bend source

Json.bend on the hub · documented module

import Base# Json: JSON values and their text (aeson's job in the Haskell backend; the# typed codecs are in Codec.bend).## What a value means: null, a truth value, a number (a decimal, as written:# its sign, integer digits, fraction digits and exponent), a string (a# sequence of Unicode code points), an array (a list of values) or an object# (a finite map from strings to values). An object is held as its keys and# its values, two lists in the same order (so that a def over a list of# values reaches every value inside: Bend recurses only into parts).## `js.show` writes a value as aeson does: no spaces; in a string `"` and `\`# escaped, `\n` `\r` `\t` as such, every other code point below U+0020 as# `\u00xx` (lower-case hex), the rest as itself. `js.parse` reads JSON text# (RFC 8259) back with a machine that eats one character at a time# (`js.step`), so it is a fold over the text: reading two pieces one after# the other is reading them joined. LAWS.bend: what `js.show` writes reads# back as the value.## Every def is prefixed `js.` (a program's defs share one namespace).# A decimal digittype js.D is Data:  js.D0{}  js.D1{}  js.D2{}  js.D3{}  js.D4{}  js.D5{}  js.D6{}  js.D7{}  js.D8{}  js.D9{}# an exponent: none, or its sign and digits (one at least)type js.Exp is Data:  js.ENone{}  js.ESome{neg: Bool, d: js.D, ds: List<&2, js.D>}# a number as written: its sign, integer digits (one at least: d, then ds),# fraction digits (none: no point) and exponenttype js.Num is Data:  js.N{neg: Bool, d: js.D, ds: List<&2, js.D>, frac: List<&2, js.D>, exp: js.Exp}type js.Json is Data:  js.Null{}  js.Bool{b: Bool}  js.Number{n: js.Num}  js.Str{s: String}  js.Arr{xs: List<&2, js.Json>}  js.Obj{ks: List<&2, String>, vs: List<&2, js.Json>}# a choice whose branches are computed only when taken (Bool.pick computes# both)def js.if(-A: Data, b: Bool, t: Unit -> A, e: Unit -> A) -> A:  match b:    case True{}:      t(Unit{})    case False{}:      e(Unit{})# Digits# ------def js.d.code(d: js.D) -> U32:  match d:    case js.D0{}:      48    case js.D1{}:      49    case js.D2{}:      50    case js.D3{}:      51    case js.D4{}:      52    case js.D5{}:      53    case js.D6{}:      54    case js.D7{}:      55    case js.D8{}:      56    case js.D9{}:      57def js.d.val(d: js.D) -> Nat:  match d:    case js.D0{}:      0n    case js.D1{}:      1n    case js.D2{}:      2n    case js.D3{}:      3n    case js.D4{}:      4n    case js.D5{}:      5n    case js.D6{}:      6n    case js.D7{}:      7n    case js.D8{}:      8n    case js.D9{}:      9ndef js.d.lo(a: Bool, b: Bool, c: Bool, d: Bool, e: Bool) -> Maybe<&2, js.D>:  match a b c d e:    case True{} _ _ _ _:      Some{js.D0{}}    case False{} True{} _ _ _:      Some{js.D1{}}    case False{} False{} True{} _ _:      Some{js.D2{}}    case False{} False{} False{} True{} _:      Some{js.D3{}}    case False{} False{} False{} False{} True{}:      Some{js.D4{}}    case _ _ _ _ _:      None{}def js.d.hi(a: Bool, b: Bool, c: Bool, d: Bool, e: Bool) -> Maybe<&2, js.D>:  match a b c d e:    case True{} _ _ _ _:      Some{js.D5{}}    case False{} True{} _ _ _:      Some{js.D6{}}    case False{} False{} True{} _ _:      Some{js.D7{}}    case False{} False{} False{} True{} _:      Some{js.D8{}}    case False{} False{} False{} False{} True{}:      Some{js.D9{}}    case _ _ _ _ _:      None{}def js.d.half(low: Bool, +c: U32) -> Maybe<&2, js.D>:  match low:    case True{}:      js.d.lo(U32.is_eq(c, 48), U32.is_eq(c, 49), U32.is_eq(c, 50), U32.is_eq(c, 51), U32.is_eq(c, 52))    case False{}:      js.d.hi(U32.is_eq(c, 53), U32.is_eq(c, 54), U32.is_eq(c, 55), U32.is_eq(c, 56), U32.is_eq(c, 57))# the digit a character is, if it is onedef js.d.of(+c: U32) -> Maybe<&2, js.D>:  js.d.half(U32.is_lt(c, 53), c)def js.ds.put(m: Maybe<&2, js.D>, acc: List<&2, js.D>, more: List<&2, js.D> -> Maybe<&2, List<&2, js.D>>) -> Maybe<&2, List<&2, js.D>>:  match m:    case None{}:      None{}    case Some{d}:      more(d <> acc)def js.ds.go(cs: String, acc: List<&2, js.D>) -> Maybe<&2, List<&2, js.D>>:  match cs:    case SNil{}:      Some{List.reverse(&2, js.D, acc)}    case SCon{Chr{+c}, t}:      js.ds.put(js.d.of(c), acc, a => js.ds.go(t, a))# a decimal's digits, if it is one (no sign)def js.ds.of(s: String) -> Maybe<&2, List<&2, js.D>>:  js.ds.go(s, [])# a digit string's valuedef js.ds.val(ds: List<&2, js.D>, +acc: Nat) -> Nat:  match ds:    case Nil{}:      acc    case Con{d, t}:      js.ds.val(t, Nat.add(Nat.mul(acc, 10n), js.d.val(d)))# Numbers# -------def js.n.of.ds(m: Maybe<&2, List<&2, js.D>>) -> js.Num:  match m:    case Some{Con{d, t}}:      js.N{False{}, d, t, [], js.ENone{}}    case x:      js.N{False{}, js.D0{}, [], [], js.ENone{}}# a natural number as aeson writes onedef js.n.nat(n: Nat) -> js.Num:  js.n.of.ds(js.ds.of(Nat.show(n)))def js.nat(n: Nat) -> js.Json:  js.Number{js.n.nat(n)}def js.u32(+n: U32) -> js.Json:  js.Number{js.n.of.ds(js.ds.of(U32.show(n)))}# a whole number's value: its integer digits, the fraction's digits moved# in by the exponent, if that leaves no fraction that is not zerodef js.zeros(ds: List<&2, js.D>) -> Bool:  match ds:    case Nil{}:      True{}    case Con{js.D0{}, t}:      js.zeros(t)    case Con{d, t}:      False{}def js.shift(k: Nat, ds: List<&2, js.D>, fs: List<&2, js.D>) -> Maybe<&2, Nat>:  match k fs:    case 0n fs2:      Bool.pick(Maybe<&2, Nat>, js.zeros(fs2), Some{js.ds.val(ds, 0n)}, None{})    case 1n+p Nil{}:      js.shift(p, List.append(&2, js.D, ds, [js.D0{}]), [])    case 1n+p Con{f, t}:      js.shift(p, List.append(&2, js.D, ds, [f]), t)def js.n.whole.e(neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, e: js.Exp) -> Maybe<&2, Nat>:  match neg e:    case False{} js.ENone{}:      js.shift(0n, ds, fs)    case False{} js.ESome{False{}, d, eds}:      js.shift(js.ds.val(d <> eds, 0n), ds, fs)    case x y:      None{}# the natural number a number is, if it is one (aeson's toBoundedInteger,# for the non-negative)def js.n.whole(n: js.Num) -> Maybe<&2, Nat>:  match n:    case js.N{neg, d, ds, fs, e}:      js.n.whole.e(neg, d <> ds, fs, e)# Writing# -------def js.digits(ds: List<&2, js.D>, out: String) -> String:  match ds:    case Nil{}:      out    case Con{d, t}:      SCon{Chr{js.d.code(d)}, js.digits(t, out)}def js.minus(neg: Bool, out: String) -> String:  match neg:    case True{}:      SCon{Chr{45}, out}    case False{}:      outdef js.frac(fs: List<&2, js.D>, out: String) -> String:  match fs:    case Nil{}:      out    case Con{d, t}:      SCon{Chr{46}, js.digits(d <> t, out)}def js.exp(e: js.Exp, out: String) -> String:  match e:    case js.ENone{}:      out    case js.ESome{neg, d, ds}:      SCon{Chr{101}, js.minus(neg, js.digits(d <> ds, out))}# a number's text, before outdef js.num(n: js.Num, out: String) -> String:  match n:    case js.N{neg, d, ds, fs, e}:      js.minus(neg, js.digits(d <> ds, js.frac(fs, js.exp(e, out))))# A string's characters, escaped. The 32 code points below U+0020 are# picked out one by one (js.c0 .. js.c31), so that what each is written as# is fixed text; one that is none of them goes on as a character from# U+0020 up would (it cannot happen, but the proof need not know that).def js.esc.hi(q: Bool, b: Bool, +c: U32, out: String) -> String:  match q b:    case True{} _:      SCon{Chr{92}, SCon{Chr{34}, out}}    case False{} True{}:      SCon{Chr{92}, SCon{Chr{92}, out}}    case False{} False{}:      SCon{Chr{c}, out}def js.c31(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{102}, out}}}}}}    case False{}:      js.esc.hi(U32.is_eq(c, 34), U32.is_eq(c, 92), c, out)def js.c30(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{101}, out}}}}}}    case False{}:      js.c31(U32.is_eq(c, 31), c, out)def js.c29(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{100}, out}}}}}}    case False{}:      js.c30(U32.is_eq(c, 30), c, out)def js.c28(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{99}, out}}}}}}    case False{}:      js.c29(U32.is_eq(c, 29), c, out)def js.c27(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{98}, out}}}}}}    case False{}:      js.c28(U32.is_eq(c, 28), c, out)def js.c26(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{97}, out}}}}}}    case False{}:      js.c27(U32.is_eq(c, 27), c, out)def js.c25(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{57}, out}}}}}}    case False{}:      js.c26(U32.is_eq(c, 26), c, out)def js.c24(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{56}, out}}}}}}    case False{}:      js.c25(U32.is_eq(c, 25), c, out)def js.c23(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{55}, out}}}}}}    case False{}:      js.c24(U32.is_eq(c, 24), c, out)def js.c22(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{54}, out}}}}}}    case False{}:      js.c23(U32.is_eq(c, 23), c, out)def js.c21(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{53}, out}}}}}}    case False{}:      js.c22(U32.is_eq(c, 22), c, out)def js.c20(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{52}, out}}}}}}    case False{}:      js.c21(U32.is_eq(c, 21), c, out)def js.c19(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{51}, out}}}}}}    case False{}:      js.c20(U32.is_eq(c, 20), c, out)def js.c18(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{50}, out}}}}}}    case False{}:      js.c19(U32.is_eq(c, 19), c, out)def js.c17(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{49}, out}}}}}}    case False{}:      js.c18(U32.is_eq(c, 18), c, out)def js.c16(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, SCon{Chr{48}, out}}}}}}    case False{}:      js.c17(U32.is_eq(c, 17), c, out)def js.c15(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{102}, out}}}}}}    case False{}:      js.c16(U32.is_eq(c, 16), c, out)def js.c14(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{101}, out}}}}}}    case False{}:      js.c15(U32.is_eq(c, 15), c, out)def js.c13(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{114}, out}}    case False{}:      js.c14(U32.is_eq(c, 14), c, out)def js.c12(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{99}, out}}}}}}    case False{}:      js.c13(U32.is_eq(c, 13), c, out)def js.c11(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{98}, out}}}}}}    case False{}:      js.c12(U32.is_eq(c, 12), c, out)def js.c10(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{110}, out}}    case False{}:      js.c11(U32.is_eq(c, 11), c, out)def js.c9(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{116}, out}}    case False{}:      js.c10(U32.is_eq(c, 10), c, out)def js.c8(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{56}, out}}}}}}    case False{}:      js.c9(U32.is_eq(c, 9), c, out)def js.c7(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{55}, out}}}}}}    case False{}:      js.c8(U32.is_eq(c, 8), c, out)def js.c6(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{54}, out}}}}}}    case False{}:      js.c7(U32.is_eq(c, 7), c, out)def js.c5(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{53}, out}}}}}}    case False{}:      js.c6(U32.is_eq(c, 6), c, out)def js.c4(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{52}, out}}}}}}    case False{}:      js.c5(U32.is_eq(c, 5), c, out)def js.c3(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{51}, out}}}}}}    case False{}:      js.c4(U32.is_eq(c, 4), c, out)def js.c2(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{50}, out}}}}}}    case False{}:      js.c3(U32.is_eq(c, 3), c, out)def js.c1(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{49}, out}}}}}}    case False{}:      js.c2(U32.is_eq(c, 2), c, out)def js.c0(hit: Bool, +c: U32, out: String) -> String:  match hit:    case True{}:      SCon{Chr{92}, SCon{Chr{117}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, SCon{Chr{48}, out}}}}}}    case False{}:      js.c1(U32.is_eq(c, 1), c, out)def js.esc.char(low: Bool, +c: U32, out: String) -> String:  match low:    case True{}:      js.c0(U32.is_eq(c, 0), c, out)    case False{}:      js.esc.hi(U32.is_eq(c, 34), U32.is_eq(c, 92), c, out)# s escaped, before outdef js.esc(s: String, out: String) -> String:  match s:    case SNil{}:      out    case SCon{Chr{+c}, t}:      js.esc.char(U32.is_lt(c, 32), c, js.esc(t, out))# s quoted, before outdef js.quote(s: String, out: String) -> String:  SCon{Chr{34}, js.esc(s, SCon{Chr{34}, out})}# Values. A list of values is written by one def that goes into each value# it meets (an array's values, an object's), with the keys an object pairs# them with: `body` writes what follows a separator and a key, given the keys# left.def js.lead.key(ks: List<&2, String>, body: List<&2, String> -> String) -> String:  match ks:    case Nil{}:      js.quote("", SCon{Chr{58}, body([])})    case Con{k, kt}:      js.quote(k, SCon{Chr{58}, body(kt)})def js.lead.obj(obj: Bool, ks: List<&2, String>, body: List<&2, String> -> String) -> String:  match obj:    case True{}:      js.lead.key(ks, body)    case False{}:      body(ks)def js.lead(first: Bool, obj: Bool, ks: List<&2, String>, body: List<&2, String> -> String) -> String:  match first:    case True{}:      js.lead.obj(obj, ks, body)    case False{}:      SCon{Chr{44}, js.lead.obj(obj, ks, body)}def js.bool(b: Bool, out: String) -> String:  match b:    case True{}:      "true" ++ out    case False{}:      "false" ++ out# The values xs, each after its key ks (in an object: obj), comma between# (first: none before the first), before out.def js.items(xs: List<&2, js.Json>, ks: List<&2, String>, +obj: Bool, first: Bool, out: String) -> String:  match xs:    case Nil{}:      out    case Con{js.Null{}, t}:      js.lead(first, obj, ks, kt => "null" ++ js.items(t, kt, obj, False{}, out))    case Con{js.Bool{b}, t}:      js.lead(first, obj, ks, kt => js.bool(b, js.items(t, kt, obj, False{}, out)))    case Con{js.Number{n}, t}:      js.lead(first, obj, ks, kt => js.num(n, js.items(t, kt, obj, False{}, out)))    case Con{js.Str{s}, t}:      js.lead(first, obj, ks, kt => js.quote(s, js.items(t, kt, obj, False{}, out)))    case Con{js.Arr{ys}, t}:      js.lead(first, obj, ks, kt => SCon{Chr{91}, js.items(ys, [], False{}, True{}, SCon{Chr{93}, js.items(t, kt, obj, False{}, out)})})    case Con{js.Obj{yks, yvs}, t}:      js.lead(first, obj, ks, kt => SCon{Chr{123}, js.items(yvs, yks, True{}, True{}, SCon{Chr{125}, js.items(t, kt, obj, False{}, out)})})# a value's text, as aeson writes itdef js.show(j: js.Json) -> String:  js.items([j], [], False{}, True{}, "")# Reading# -------# What a reader is in: inside arrays and objects (the frames: the values# so far, reversed; an object's keys too, and the key the next value goes# under), and what it wants next.type js.Fr is Data:  js.FArr{acc: List<&2, js.Json>}  js.FObj{ks: List<&2, String>, vs: List<&2, js.Json>, key: String}# where a number is: after its "-"; in its integer digits; after its ".";# in its fraction; after its "e"; after the exponent's sign; in the exponenttype js.Ph is Data:  js.PSign{}  js.PInt{}  js.PDot{}  js.PFrac{}  js.PE{}  js.PESign{}  js.PExp{}type js.M is Data:  js.MVal{}  js.MFirst{}  js.MKey{}  js.MKey1{}  js.MColon{}  js.MAfter{}  js.MStr{acc: String, key: Bool, hi: U32}  js.MEsc{acc: String, key: Bool, hi: U32}  js.MHex{acc: String, key: Bool, hi: U32, n: U32, v: U32}  js.MLit{want: String, j: js.Json}  js.MNum{neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, eneg: Bool, es: List<&2, js.D>, ph: js.Ph}  js.MDone{j: js.Json}  js.MBad{}type js.St is Data:  js.St{stack: List<&2, js.Fr>, m: js.M}def js.bad() -> js.St:  js.St{[], js.MBad{}}def js.ws(+c: U32) -> Bool:  U32.is_eq(c, 32) || (U32.is_eq(c, 10) || (U32.is_eq(c, 13) || U32.is_eq(c, 9)))# a value done: into the frame it is in, or the whole text'sdef js.place(stack: List<&2, js.Fr>, j: js.Json) -> js.St:  match stack:    case Nil{}:      js.St{[], js.MDone{j}}    case Con{js.FArr{acc}, rest}:      js.St{js.FArr{j <> acc} <> rest, js.MAfter{}}    case Con{js.FObj{ks, vs, k}, rest}:      js.St{js.FObj{k <> ks, j <> vs, ""} <> rest, js.MAfter{}}def js.close.arr(stack: List<&2, js.Fr>) -> js.St:  match stack:    case Con{js.FArr{acc}, rest}:      js.place(rest, js.Arr{List.reverse(&2, js.Json, acc)})    case x:      js.bad()def js.close.obj(stack: List<&2, js.Fr>) -> js.St:  match stack:    case Con{js.FObj{ks, vs, k}, rest}:      js.place(rest, js.Obj{List.reverse(&2, String, ks), List.reverse(&2, js.Json, vs)})    case x:      js.bad()def js.comma(stack: List<&2, js.Fr>) -> js.St:  match stack:    case Con{js.FArr{acc}, rest}:      js.St{js.FArr{acc} <> rest, js.MVal{}}    case Con{js.FObj{ks, vs, k}, rest}:      js.St{js.FObj{ks, vs, k} <> rest, js.MKey1{}}    case Nil{}:      js.bad()def js.setkey(stack: List<&2, js.Fr>, k: String) -> js.St:  match stack:    case Con{js.FObj{ks, vs, old}, rest}:      js.St{js.FObj{ks, vs, k} <> rest, js.MColon{}}    case x:      js.bad()def js.start.digit(m: Maybe<&2, js.D>, stack: List<&2, js.Fr>) -> js.St:  match m:    case None{}:      js.bad()    case Some{d}:      js.St{stack, js.MNum{False{}, [d], [], False{}, [], js.PInt{}}}def js.start.at(q: Bool, a: Bool, o: Bool, t: Bool, f: Bool, n: Bool, m: Bool, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match q a o t f n m:    case True{} _ _ _ _ _ _:      js.St{stack, js.MStr{"", False{}, 0}}    case False{} True{} _ _ _ _ _:      js.St{js.FArr{[]} <> stack, js.MFirst{}}    case False{} False{} True{} _ _ _ _:      js.St{js.FObj{[], [], ""} <> stack, js.MKey{}}    case False{} False{} False{} True{} _ _ _:      js.St{stack, js.MLit{"rue", js.Bool{True{}}}}    case False{} False{} False{} False{} True{} _ _:      js.St{stack, js.MLit{"alse", js.Bool{False{}}}}    case False{} False{} False{} False{} False{} True{} _:      js.St{stack, js.MLit{"ull", js.Null{}}}    case False{} False{} False{} False{} False{} False{} True{}:      js.St{stack, js.MNum{True{}, [], [], False{}, [], js.PSign{}}}    case False{} False{} False{} False{} False{} False{} False{}:      js.start.digit(js.d.of(c), stack)# the first character of a valuedef js.start(stack: List<&2, js.Fr>, +c: U32) -> js.St:  js.start.at(U32.is_eq(c, 34), U32.is_eq(c, 91), U32.is_eq(c, 123), U32.is_eq(c, 116), U32.is_eq(c, 102), U32.is_eq(c, 110), U32.is_eq(c, 45), stack, c)def js.on.val(w: Bool, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match w:    case True{}:      js.St{stack, js.MVal{}}    case False{}:      js.start(stack, c)def js.on.first(w: Bool, close: Bool, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match w close:    case True{} _:      js.St{stack, js.MFirst{}}    case False{} True{}:      js.close.arr(stack)    case False{} False{}:      js.start(stack, c)def js.on.key(w: Bool, q: Bool, close: Bool, stack: List<&2, js.Fr>) -> js.St:  match w q close:    case True{} _ _:      js.St{stack, js.MKey{}}    case False{} True{} _:      js.St{stack, js.MStr{"", True{}, 0}}    case False{} False{} True{}:      js.close.obj(stack)    case False{} False{} False{}:      js.bad()def js.on.key1(w: Bool, q: Bool, stack: List<&2, js.Fr>) -> js.St:  match w q:    case True{} _:      js.St{stack, js.MKey1{}}    case False{} True{}:      js.St{stack, js.MStr{"", True{}, 0}}    case False{} False{}:      js.bad()def js.on.colon(w: Bool, k: Bool, stack: List<&2, js.Fr>) -> js.St:  match w k:    case True{} _:      js.St{stack, js.MColon{}}    case False{} True{}:      js.St{stack, js.MVal{}}    case False{} False{}:      js.bad()def js.on.after.at(w: Bool, comma: Bool, ca: Bool, co: Bool, stack: List<&2, js.Fr>) -> js.St:  match w comma ca co:    case True{} _ _ _:      js.St{stack, js.MAfter{}}    case False{} True{} _ _:      js.comma(stack)    case False{} False{} True{} _:      js.close.arr(stack)    case False{} False{} False{} True{}:      js.close.obj(stack)    case False{} False{} False{} False{}:      js.bad()def js.on.done(w: Bool, j: js.Json) -> js.St:  match w:    case True{}:      js.St{[], js.MDone{j}}    case False{}:      js.bad()# after a value: a separator, a closer, or (the whole text's) the enddef js.on.after(st: js.St, +c: U32) -> js.St:  match st:    case js.St{stack, js.MAfter{}}:      js.on.after.at(js.ws(c), U32.is_eq(c, 44), U32.is_eq(c, 93), U32.is_eq(c, 125), stack)    case js.St{Nil{}, js.MDone{j}}:      js.on.done(js.ws(c), j)    case x:      js.bad()# Strings: hi is a high surrogate read as \uXXXX and waiting for its low# half (0: none); one left alone is U+FFFD.def js.flush.at(none: Bool, acc: String) -> String:  match none:    case True{}:      acc    case False{}:      SCon{Chr{65533}, acc}def js.flush(+hi: U32, acc: String) -> String:  js.flush.at(U32.is_eq(hi, 0), acc)def js.push(+hi: U32, +c: U32, acc: String) -> String:  SCon{Chr{c}, js.flush(hi, acc)}def js.str.end(key: Bool, stack: List<&2, js.Fr>, s: String) -> js.St:  match key:    case True{}:      js.setkey(stack, s)    case False{}:      js.place(stack, js.Str{s})def js.in.str(q: Bool, b: Bool, stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +c: U32) -> js.St:  match q b:    case True{} _:      js.str.end(key, stack, String.reverse(js.flush(hi, acc)))    case False{} True{}:      js.St{stack, js.MEsc{acc, key, hi}}    case False{} False{}:      js.St{stack, js.MStr{js.push(hi, c, acc), key, 0}}def js.esc.of.at(q: Bool, b: Bool, s: Bool, bb: Bool, f: Bool, n: Bool, r: Bool, t: Bool) -> U32:  match q b s bb f n r t:    case True{} _ _ _ _ _ _ _:      34    case False{} True{} _ _ _ _ _ _:      92    case False{} False{} True{} _ _ _ _ _:      47    case False{} False{} False{} True{} _ _ _ _:      8    case False{} False{} False{} False{} True{} _ _ _:      12    case False{} False{} False{} False{} False{} True{} _ _:      10    case False{} False{} False{} False{} False{} False{} True{} _:      13    case False{} False{} False{} False{} False{} False{} False{} True{}:      9    case _ _ _ _ _ _ _ _:      4294967295# what the character after a backslash stands for (4294967295: nothing)def js.esc.of(+c: U32) -> U32:  js.esc.of.at(U32.is_eq(c, 34), U32.is_eq(c, 92), U32.is_eq(c, 47), U32.is_eq(c, 98), U32.is_eq(c, 102), U32.is_eq(c, 110), U32.is_eq(c, 114), U32.is_eq(c, 116))def js.in.esc.at(u: Bool, bad: Bool, stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +x: U32) -> js.St:  match u bad:    case True{} _:      js.St{stack, js.MHex{acc, key, hi, 4, 0}}    case False{} True{}:      js.bad()    case False{} False{}:      js.St{stack, js.MStr{js.push(hi, x, acc), key, 0}}def js.in.esc(stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +c: U32) -> js.St:  js.in.esc.at(U32.is_eq(c, 117), U32.is_eq(js.esc.of(c), 4294967295), stack, acc, key, hi, js.esc.of(c))def js.hexv.at(a: Bool, b: Bool, c2: Bool, d: Bool, e: Bool, f: Bool, +c: U32) -> U32:  match a b c2 d e f:    case False{} True{} _ _ _ _:      (c - 48 : U32)    case False{} False{} False{} True{} _ _:      (c - 55 : U32)    case False{} False{} False{} False{} False{} True{}:      (c - 87 : U32)    case _ _ _ _ _ _:      16# a hex digit's value (16: none)def js.hexv(+c: U32) -> U32:  js.hexv.at(U32.is_lt(c, 48), U32.is_lt(c, 58), U32.is_lt(c, 65), U32.is_lt(c, 71), U32.is_lt(c, 97), U32.is_lt(c, 103), c)def js.pair.low(none: Bool, +hi: U32, +u: U32, acc: String) -> String:  match none:    case True{}:      SCon{Chr{65533}, acc}    case False{}:      SCon{Chr{(65536 + U32.shln((hi - 55296 : U32), 10n) + (u - 56320 : U32) : U32)}, acc}def js.unit.at(high: Bool, low: Bool, stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +u: U32) -> js.St:  match high low:    case True{} _:      js.St{stack, js.MStr{js.flush(hi, acc), key, u}}    case False{} True{}:      js.St{stack, js.MStr{js.pair.low(U32.is_eq(hi, 0), hi, u, acc), key, 0}}    case False{} False{}:      js.St{stack, js.MStr{js.push(hi, u, acc), key, 0}}# a code unit read as \uXXXXdef js.unit(stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +u: U32) -> js.St:  js.unit.at(U32.is_ge(u, 55296) && U32.is_lt(u, 56320), U32.is_ge(u, 56320) && U32.is_lt(u, 57344), stack, acc, key, hi, u)def js.in.hex.at(bad: Bool, last: Bool, stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +n: U32, +v: U32) -> js.St:  match bad last:    case True{} _:      js.bad()    case False{} True{}:      js.unit(stack, acc, key, hi, v)    case False{} False{}:      js.St{stack, js.MHex{acc, key, hi, (n - 1 : U32), v}}def js.in.hex(stack: List<&2, js.Fr>, acc: String, key: Bool, +hi: U32, +n: U32, +v: U32, +h: U32) -> js.St:  js.in.hex.at(U32.is_eq(h, 16), U32.is_eq(n, 1), stack, acc, key, hi, n, (v * 16 + h : U32))# true, false, null: the letters still wanteddef js.lit.more(t: String, j: js.Json, stack: List<&2, js.Fr>) -> js.St:  match t:    case SNil{}:      js.place(stack, j)    case SCon{h, r}:      js.St{stack, js.MLit{SCon{h, r}, j}}def js.lit.at(hit: Bool, t: String, j: js.Json, stack: List<&2, js.Fr>) -> js.St:  match hit:    case True{}:      js.lit.more(t, j, stack)    case False{}:      js.bad()def js.in.lit(want: String, j: js.Json, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match want:    case SNil{}:      js.bad()    case SCon{Chr{+w}, t}:      js.lit.at(U32.is_eq(c, w), t, j, stack)# Numbers: a number ends at the first character that cannot go on it, which# then is read as what follows a value.def js.num.e(has: Bool, eneg: Bool, es: List<&2, js.D>) -> Maybe<&2, js.Exp>:  match has es:    case False{} x:      Some{js.ENone{}}    case True{} Con{d, t}:      Some{js.ESome{eneg, d, t}}    case True{} Nil{}:      None{}def js.num.mk(neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, e: Maybe<&2, js.Exp>) -> Maybe<&2, js.Num>:  match ds e:    case Con{d, t} Some{x}:      Some{js.N{neg, d, t, fs, x}}    case x y:      None{}def js.num.make(neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, eneg: Bool, es: List<&2, js.D>, has: Bool) -> Maybe<&2, js.Num>:  js.num.mk(neg, List.reverse(&2, js.D, ds), List.reverse(&2, js.D, fs), js.num.e(has, eneg, List.reverse(&2, js.D, es)))# whether a number may end where it is, and whether it has an exponentdef js.ph.end(ph: js.Ph) -> Bool:  match ph:    case js.PInt{}:      True{}    case js.PFrac{}:      True{}    case js.PExp{}:      True{}    case x:      False{}def js.ph.exp(ph: js.Ph) -> Bool:  match ph:    case js.PExp{}:      True{}    case x:      False{}def js.num.fin(neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, eneg: Bool, es: List<&2, js.D>, +ph: js.Ph) -> Maybe<&2, js.Num>:  Bool.pick(Maybe<&2, js.Num>, js.ph.end(ph), js.num.make(neg, ds, fs, eneg, es, js.ph.exp(ph)), None{})def js.num.end(m: Maybe<&2, js.Num>, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match m:    case None{}:      js.bad()    case Some{n}:      js.on.after(js.place(stack, js.Number{n}), c)def js.num.digit(ph: js.Ph, d: js.D, neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, eneg: Bool, es: List<&2, js.D>, stack: List<&2, js.Fr>) -> js.St:  match ph:    case js.PSign{}:      js.St{stack, js.MNum{neg, [d], fs, eneg, es, js.PInt{}}}    case js.PInt{}:      js.St{stack, js.MNum{neg, d <> ds, fs, eneg, es, js.PInt{}}}    case js.PDot{}:      js.St{stack, js.MNum{neg, ds, [d], eneg, es, js.PFrac{}}}    case js.PFrac{}:      js.St{stack, js.MNum{neg, ds, d <> fs, eneg, es, js.PFrac{}}}    case js.PE{}:      js.St{stack, js.MNum{neg, ds, fs, eneg, [d], js.PExp{}}}    case js.PESign{}:      js.St{stack, js.MNum{neg, ds, fs, eneg, [d], js.PExp{}}}    case js.PExp{}:      js.St{stack, js.MNum{neg, ds, fs, eneg, d <> es, js.PExp{}}}def js.num.other(ph: js.Ph, dot: Bool, e: Bool, sign: Bool, mneg: Bool, neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, eneg: Bool, es: List<&2, js.D>, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match ph dot e sign:    case js.PInt{} True{} _ _:      js.St{stack, js.MNum{neg, ds, fs, eneg, es, js.PDot{}}}    case js.PInt{} False{} True{} _:      js.St{stack, js.MNum{neg, ds, fs, eneg, es, js.PE{}}}    case js.PFrac{} _ True{} _:      js.St{stack, js.MNum{neg, ds, fs, eneg, es, js.PE{}}}    case js.PE{} _ _ True{}:      js.St{stack, js.MNum{neg, ds, fs, mneg, es, js.PESign{}}}    case js.PInt{} False{} False{} _:      js.num.end(js.num.make(neg, ds, fs, eneg, es, False{}), stack, c)    case js.PFrac{} _ False{} _:      js.num.end(js.num.make(neg, ds, fs, eneg, es, False{}), stack, c)    case js.PExp{} _ _ _:      js.num.end(js.num.make(neg, ds, fs, eneg, es, True{}), stack, c)    case x y z w:      js.bad()def js.in.num(dm: Maybe<&2, js.D>, ph: js.Ph, neg: Bool, ds: List<&2, js.D>, fs: List<&2, js.D>, eneg: Bool, es: List<&2, js.D>, stack: List<&2, js.Fr>, +c: U32) -> js.St:  match dm:    case Some{d}:      js.num.digit(ph, d, neg, ds, fs, eneg, es, stack)    case None{}:      js.num.other(ph, U32.is_eq(c, 46), U32.is_eq(c, 101) || U32.is_eq(c, 69), U32.is_eq(c, 43) || U32.is_eq(c, 45), U32.is_eq(c, 45),        neg, ds, fs, eneg, es, stack, c)# One character read.def js.step(st: js.St, +c: U32) -> js.St:  match st:    case js.St{stack, js.MVal{}}:      js.on.val(js.ws(c), stack, c)    case js.St{stack, js.MFirst{}}:      js.on.first(js.ws(c), U32.is_eq(c, 93), stack, c)    case js.St{stack, js.MKey{}}:      js.on.key(js.ws(c), U32.is_eq(c, 34), U32.is_eq(c, 125), stack)    case js.St{stack, js.MKey1{}}:      js.on.key1(js.ws(c), U32.is_eq(c, 34), stack)    case js.St{stack, js.MColon{}}:      js.on.colon(js.ws(c), U32.is_eq(c, 58), stack)    case js.St{stack, js.MAfter{}}:      js.on.after.at(js.ws(c), U32.is_eq(c, 44), U32.is_eq(c, 93), U32.is_eq(c, 125), stack)    case js.St{stack, js.MStr{acc, key, +hi}}:      js.in.str(U32.is_eq(c, 34), U32.is_eq(c, 92), stack, acc, key, hi, c)    case js.St{stack, js.MEsc{acc, key, +hi}}:      js.in.esc(stack, acc, key, hi, c)    case js.St{stack, js.MHex{acc, key, +hi, +n, +v}}:      js.in.hex(stack, acc, key, hi, n, v, js.hexv(c))    case js.St{stack, js.MLit{want, j}}:      js.in.lit(want, j, stack, c)    case js.St{stack, js.MNum{neg, ds, fs, eneg, es, ph}}:      js.in.num(js.d.of(c), ph, neg, ds, fs, eneg, es, stack, c)    case js.St{Nil{}, js.MDone{j}}:      js.on.done(js.ws(c), j)    case x:      js.bad()# the text read, from a state ondef js.run(cs: String, st: js.St) -> js.St:  match cs:    case SNil{}:      st    case SCon{Chr{+c}, t}:      js.run(t, js.step(st, c))def js.fin.num(m: Maybe<&2, js.Num>) -> Maybe<&2, js.Json>:  match m:    case None{}:      None{}    case Some{n}:      Some{js.Number{n}}# what a reader that has read the whole text has readdef js.fin(st: js.St) -> Maybe<&2, js.Json>:  match st:    case js.St{Nil{}, js.MDone{j}}:      Some{j}    case js.St{Nil{}, js.MNum{neg, ds, fs, eneg, es, ph}}:      js.fin.num(js.num.fin(neg, ds, fs, eneg, es, ph))    case x:      None{}# JSON text readdef js.parse(s: String) -> Maybe<&2, js.Json>:  js.fin(js.run(s, js.St{[], js.MVal{}}))# Looking inside# --------------def js.find(ks: List<&2, String>, vs: List<&2, js.Json>, +k: String) -> Maybe<&2, js.Json>:  match ks vs:    case Con{+k2, kt} Con{v, vt}:      js.if(Maybe<&2, js.Json>, String.eq(k2, k), u => Some{v}, u => js.find(kt, vt, k))    case x y:      None{}# an object's value under a key (the first; None: no such key, or no object)def js.get(j: js.Json, +k: String) -> Maybe<&2, js.Json>:  match j:    case js.Obj{ks, vs}:      js.find(ks, vs, k)    case x:      None{}# What aeson's readers in the Haskell backend make of a value that may be# missing: a string (a number as its text), a natural number (0 when it is# none), a truth value (False when none), strings (an array's strings).def js.text(m: Maybe<&2, js.Json>) -> String:  match m:    case Some{js.Str{s}}:      s    case Some{js.Number{n}}:      js.num(n, "")    case x:      ""def js.whole.of(m: Maybe<&2, Nat>) -> Nat:  match m:    case Some{n}:      n    case None{}:      0ndef js.natural(m: Maybe<&2, js.Json>) -> Nat:  match m:    case Some{js.Number{n}}:      js.whole.of(js.n.whole(n))    case x:      0ndef js.truth(m: Maybe<&2, js.Json>) -> Bool:  match m:    case Some{js.Bool{b}}:      b    case x:      False{}def js.strs.go(xs: List<&2, js.Json>) -> List<&2, String>:  match xs:    case Nil{}:      []    case Con{js.Str{s}, t}:      s <> js.strs.go(t)    case Con{x, t}:      js.strs.go(t)def js.texts(m: Maybe<&2, js.Json>) -> List<&2, String>:  match m:    case Some{js.Arr{xs}}:      js.strs.go(xs)    case x:      []