Real.bend source
Real.bend on the hub · documented module
import Baseimport ./Json.bend as J# Real: a time as a Double, written as Haskell writes one (the stamp the# Haskell backend sends is a file's time, `realToFrac` of POSIX seconds, a# Double that aeson writes in its shortest form: 1.7946233932029915e9).## What it means: x = secs + ns/10^9; the Double nearest x (ties to even),# and of the decimals that read back as that Double the shortest, the# nearest (Haskell's floatToDigits); written with one digit before the point# and an exponent, as `show` writes numbers past 10^7. Exact, for times from# 2^30 to 2^31 seconds (2004 to 2038): their Doubles are multiples of 2^-22,# and Bend's Nat holds 2^48, so numbers past that are lists of 16-bit limbs.## Every def is prefixed `rl.` (a program's defs share one namespace).def rl.if(-A: Data, b: Bool, t: Unit -> A, e: Unit -> A) -> A: match b: case True{}: t(Unit{}) case False{}: e(Unit{})# Limbs: little-endian base 2^16def rl.limbs(+n: Nat) -> List<&2, Nat>: [Nat.mod(n, 65536n), Nat.mod(Nat.div(n, 65536n), 65536n), Nat.div(Nat.div(n, 65536n), 65536n)]def rl.mul.k(a: List<&2, Nat>, +k: Nat, +carry: Nat) -> List<&2, Nat>: match a: case Nil{}: Bool.pick(List<&2, Nat>, Nat.is_eq(carry, 0n), [], rl.limbs(carry)) case Con{+x, t}: Nat.mod(Nat.add(Nat.mul(x, k), carry), 65536n) <> rl.mul.k(t, k, Nat.div(Nat.add(Nat.mul(x, k), carry), 65536n))def rl.add.k(a: List<&2, Nat>, +k: Nat) -> List<&2, Nat>: match a: case Nil{}: Bool.pick(List<&2, Nat>, Nat.is_eq(k, 0n), [], rl.limbs(k)) case Con{+x, t}: Nat.mod(Nat.add(x, k), 65536n) <> rl.add.k(t, Nat.div(Nat.add(x, k), 65536n))def rl.mul10(p: Nat, a: List<&2, Nat>) -> List<&2, Nat>: match p: case 0n: a case 1n+q: rl.mul10(q, rl.mul.k(a, 10n, 0n))# a number divided by a small one (below 2^30): quotient and remaindertype rl.QR is Data: rl.QR{q: List<&2, Nat>, r: Nat}def rl.qr.cons(+x: Nat, d: rl.QR) -> rl.QR: match d: case rl.QR{q, r}: rl.QR{x <> q, r}# (from the highest limb: the quotient's limbs built highest first)def rl.div.go(hi: List<&2, Nat>, +k: Nat, +r: Nat, q: List<&2, Nat>) -> rl.QR: match hi: case Nil{}: rl.QR{q, r} case Con{+x, t}: rl.div.go(t, k, Nat.mod(Nat.add(Nat.mul(r, 65536n), x), k), Nat.div(Nat.add(Nat.mul(r, 65536n), x), k) <> q)def rl.div(a: List<&2, Nat>, +k: Nat) -> rl.QR: rl.div.go(List.reverse(&2, Nat, a), k, 0n, [])def rl.zero(a: List<&2, Nat>) -> Bool: match a: case Nil{}: True{} case Con{+x, t}: Nat.is_eq(x, 0n) && rl.zero(t)def rl.digits.qr(d: rl.QR, acc: String, more: List<&2, Nat> -> String -> String) -> String: match d: case rl.QR{q, r}: more(q, SCon{Chr{(U32.from_nat(r) + 48 : U32)}, acc})# a number's decimal digits ("" for 0)def rl.digits.go(fuel: Nat, +a: List<&2, Nat>, +acc: String) -> String: match fuel: case 0n: acc case 1n+f: rl.if(String, rl.zero(a), u => acc, u => rl.digits.qr(rl.div(a, 10n), acc, q => s => rl.digits.go(f, q, s)))def rl.digits(+a: List<&2, Nat>) -> String: rl.digits.go(64n, a, "")# The Double nearest secs + ns/10^9, as its mantissa M (the Double is M/2^22)def rl.half(+f: Nat, +r0: Nat) -> Nat: Bool.pick(Nat, Nat.is_gt(Nat.mul(2n, r0), 1000000000n) || (Nat.is_eq(Nat.mul(2n, r0), 1000000000n) && Nat.is_eq(Nat.mod(f, 2n), 1n)), 1n, 0n)def rl.frac.r0(+f: Nat, +r0: Nat) -> Nat: Nat.add(f, rl.half(f, r0))def rl.frac.r1(+q1: Nat, +r1: Nat) -> Nat: rl.frac.r0(Nat.add(Nat.mul(q1, 65536n), Nat.div(Nat.mul(r1, 65536n), 1000000000n)), Nat.mod(Nat.mul(r1, 65536n), 1000000000n))def rl.frac(+ns: Nat) -> Nat: rl.frac.r1(Nat.div(Nat.mul(ns, 64n), 1000000000n), Nat.mod(Nat.mul(ns, 64n), 1000000000n))def rl.mantissa(+secs: Nat, +ns: Nat) -> List<&2, Nat>: rl.add.k(rl.mul.k(rl.limbs(secs), 4194304n, 0n), rl.frac(ns))# the shortest decimal digits that read back as M/2^22, and how many of# them stand before the point (10: the times above)def rl.near(+r: Nat) -> Nat: Bool.pick(Nat, Nat.is_lt(r, 2097152n), r, Nat.sub(4194304n, r))def rl.round.q(d: rl.QR, +even: Bool) -> List<&2, Nat>: match d: case rl.QR{+q, +r}: rl.if(List<&2, Nat>, Nat.is_lt(r, 2097152n), u => q, u => rl.if(List<&2, Nat>, Nat.is_gt(r, 2097152n), u => rl.add.k(q, 1n), u => Bool.pick(List<&2, Nat>, even, q, rl.add.k(q, 1n))))def rl.qr.r(d: rl.QR) -> Nat: match d: case rl.QR{q, r}: rdef rl.qr.q(d: rl.QR) -> List<&2, Nat>: match d: case rl.QR{q, r}: qdef rl.pow10(n: Nat) -> Nat: match n: case 0n: 1n case 1n+p: Nat.mul(10n, rl.pow10(p))def rl.first.limb(a: List<&2, Nat>) -> Nat: match a: case Nil{}: 0n case Con{x, t}: xdef rl.try.d(+d: rl.QR, +even: Bool, +p: Nat) -> Maybe<&2, String>: Bool.pick(Maybe<&2, String>, Nat.is_lt(Nat.mul(2n, rl.near(rl.qr.r(d))), rl.pow10(p)) || (even && Nat.is_eq(Nat.mul(2n, rl.near(rl.qr.r(d))), rl.pow10(p))), Some{rl.digits(rl.round.q(d, Nat.is_eq(Nat.mod(rl.first.limb(rl.qr.q(d)), 2n), 0n)))}, None{})# with 10 + p digits: the nearest such decimal, if it reads back as M/2^22def rl.try(+m: List<&2, Nat>, +even: Bool, +p: Nat) -> Maybe<&2, String>: rl.try.d(rl.div(rl.mul10(p, m), 4194304n), even, p)def rl.or.try(m: Maybe<&2, String>, more: Unit -> String) -> String: match m: case Some{s}: s case None{}: more(Unit{})def rl.search(fuel: Nat, +m: List<&2, Nat>, +even: Bool, +p: Nat) -> String: match fuel: case 0n: "" case 1n+f: rl.or.try(rl.try(m, even, p), u => rl.search(f, m, even, 1n+p))def rl.drop0(s: String) -> String: match s: case SCon{Chr{48}, t}: rl.drop0(t) case x: xdef rl.strip0(s: String) -> String: String.reverse(rl.drop0(String.reverse(s)))# the digits of M/2^22: its own when it is a whole number, else the shortestdef rl.shortest(+m: List<&2, Nat>) -> String: rl.if(String, Nat.is_eq(rl.qr.r(rl.div(m, 4194304n)), 0n), u => rl.strip0(rl.digits(rl.qr.q(rl.div(m, 4194304n)))), u => rl.search(9n, m, Nat.is_eq(Nat.mod(rl.first.limb(m), 2n), 0n), 0n))def rl.ds.got(m: Maybe<&2, List<&2, J.js.D>>) -> List<&2, J.js.D>: match m: case Some{ds}: ds case None{}: []def rl.num.ds(ds: List<&2, J.js.D>) -> J.js.Num: match ds: case Con{d, Nil{}}: J.js.N{False{}, d, [], [J.js.D0{}], J.js.ESome{False{}, J.js.D9{}, []}} case Con{d, t}: J.js.N{False{}, d, [], t, J.js.ESome{False{}, J.js.D9{}, []}} case Nil{}: J.js.N{False{}, J.js.D0{}, [], [J.js.D0{}], J.js.ENone{}}# secs + ns/10^9 as the Double Haskell writes (secs from 2^30 to 2^31)def rl.time(+secs: Nat, +ns: Nat) -> J.js.Num: rl.num.ds(rl.ds.got(J.js.ds.of(rl.shortest(rl.mantissa(secs, ns)))))