From 1b5be4aeeba08ae24684131e73f020f2cc747493 Mon Sep 17 00:00:00 2001 From: Nico Abril Date: Mon, 16 Nov 2026 20:30:49 +0100 Subject: [PATCH] perf(comp): defs that tail-call each other in a cycle run as one loop On the C lane a tail-call cycle across defs was never flat: each def was a segment, a call into it from an expression a frame and a return, and its callers were not flat either. A cycle is now flat when one of its defs is a head (every other def is named once in the cycle), no def of it calls another in an argument, and their other callees are flat. The head's spin holds each other def's body in place of its call, by the path a tail call to a def with one call site already takes, and a call back to the head is the spin's jump. A def that is no head gets a spin that calls the head's. Base's String.contains, String.starts_with, String.eq, U32.show, Nat.show, U32.read, Nat.read, U32.hex, String.trim_start and Map.diff are such cycles; the JS lane already ran them as one loop. Co-Authored-By: Claude Fable 5.2 --- bend2/comp.ts | 29 +- tests/run/tail_cycle_callers.bend | 351 ++++++++++++++++++++++++ tests/run/tail_cycle_owners.bend | 431 ++++++++++++++++++++++++++++++ tests/run/tail_cycle_shapes.bend | 170 ++++++++++++ 4 files changed, 976 insertions(+), 5 deletions(-) create mode 100644 tests/run/tail_cycle_callers.bend create mode 100644 tests/run/tail_cycle_owners.bend create mode 100644 tests/run/tail_cycle_shapes.bend diff --git a/bend2/comp.ts b/bend2/comp.ts index bec68f3d4..b5b0bff7b 100644 --- a/bend2/comp.ts +++ b/bend2/comp.ts @@ -1009,6 +1009,9 @@ function mat_ops(eq: (k: Name) => string): string { // fun_of raises a def's arity by the lambdas its body opens past its // parameters under every arm. A def is flat if it and its callees have no // fork, no bang call and only tail self-calls; loop_of is its tail cycle. +// A cycle of several defs is flat too if none calls another in an argument +// and one is a head: each other def is named once in the cycle, so the +// head's spin holds its body in place of that call. function fun_of(k: Name): Fun { return memo(FL.funs, k, () => { @@ -1095,11 +1098,24 @@ function loop_of(k: Name): Name[] { return memo(FL.loops, k, () => (visit(k), FL.loops.get(k)!)); } +function loop_head(h: Name): boolean { + const all = loop_of(h); + return all.every((d) => d === h || all.reduce((n, c) => + n + (FL.sites.get(c + " " + d) ?? 0), 0) === 1); +} + function flat_of(k: Name): boolean { return memo(FL.flats, k, () => { - const deps = FL.srcs.get(k); - FL.flats.set(k, false); - return deps != null && [...deps].every(flat_of); + const all = [k, ...loop_of(k)]; + all.forEach((d) => FL.flats.set(d, false)); + const ok = all.some(loop_head) && all.every((d) => { + const deps = FL.srcs.get(d); + return deps != null && [...deps].every((x) => all.includes(x) + || flat_of(x)) && !term_any(fun_of(d).h!, (s, tail) => + !tail && all.includes(term_spine(s).k!)); + }); + all.forEach((d) => FL.flats.set(d, ok)); + return ok; }); } @@ -1236,7 +1252,8 @@ function file_book(roots: Name[]): void { } if (!intr_of(s.k)) { src_add(s.k); - FL.sites.set(s.k, (FL.sites.get(s.k) ?? 0) + 1); + [s.k, d + " " + s.k].forEach((k) => + FL.sites.set(k, (FL.sites.get(k) ?? 0) + 1)); } } const ck = term_spine(s); @@ -2406,7 +2423,9 @@ function emit_body(sc: Scope, tm: HTerm, ty0: HTerm | null, args: Val[], const once = FL.sites.get(ck.k) === 1 && !ck.b && !def_foreign(ck.tld) && (ret !== BOX || sc.seg.ret === BOX); if (sc.seg.def !== ck.k && (flat_call(x) || (dst === null && once))) { - return flat_of(ck.k) ? emit_put(sc, dst, emit_spin(sc, ck)) + return flat_of(ck.k) && !(dst && loop_head(sc.seg.def) + && loop_of(ck.k).includes(sc.seg.def)) + ? emit_put(sc, dst, emit_spin(sc, ck)) : emit_body({ ...sc, def: ck.k }, h!, ck.tld!.T, emit_args(sc, ck, true), dst); } diff --git a/tests/run/tail_cycle_callers.bend b/tests/run/tail_cycle_callers.bend new file mode 100644 index 000000000..4caa50a9e --- /dev/null +++ b/tests/run/tail_cycle_callers.bend @@ -0,0 +1,351 @@ +# A tail-call cycle that runs as one loop, called from every kind of place: +# a fork's branches, a ! call, a def passed as a function, a partial +# application, a closure, a def of the cycle that answers a closure, a +# literal call the emitter runs itself. And the cycles that must stay as they +# were: one with a call to itself in an argument, one with a fork, one whose +# helper calls a def that is not flat, one a def outside calls back into. +# The last two recurse 20,000 frames deep in sixteen forked leaves: merged, +# they would ride the C stack of a pool thread. +import Base + +@unsafe +def ev.if(odd: Bool, +n: U32, steps: U32) -> U32: + match odd: + case True{}: + ev(U32.add(U32.mul(n, 3), 1), U32.add(steps, 1)) + case False{}: + ev(U32.shr(n), U32.add(steps, 1)) + +@unsafe +def ev.end(small: Bool, +n: U32, steps: U32) -> U32: + match small: + case True{}: + steps + case False{}: + ev.if(U32.is_eq(U32.and(n, 1), 1), n, steps) + +@unsafe +def ev(+n: U32, steps: U32) -> U32: + ev.end(U32.is_le(n, 1), n, steps) + +# a fork over the cycle, each branch a task +def tree(+d: Nat, +x: U32) -> U32: + match d: + case 0n: + ev(U32.add(x, 27), 0) + case 1n+p: + a b = tree(p, U32.mul(x, 2)) tree(p, U32.add(U32.mul(x, 2), 1)) + U32.add(a, b) + +# the fork's branches are the cycle's defs themselves +def both(+x: U32) -> U32: + a b = ev(x, 0) ev.if(True{}, x, 5) + U32.add(a, b) + +# a def as a function value, and a partial application of each def +def apply(f: @+n:U32 -> U32 -> U32, x: U32) -> U32: + f(x, 0) + +def twice(f: U32 -> U32, g: U32 -> U32, +x: U32) -> U32: + U32.add(f(x), g(x)) + +# a closure that captures a value and runs the cycle +def later(+k: U32) -> U32 -> U32: + x => ev(U32.add(x, k), k) + +# a def of the cycle that answers a closure calling the other +@unsafe +def cl.b(n: Nat, +k: U32) -> U32 -> U32: + match n: + case 0n: + x => U32.add(x, k) + case 1n+p: + cl.a(p, U32.add(k, 1)) + +@unsafe +def cl.a(n: Nat, +k: U32) -> U32 -> U32: + match n: + case 0n: + x => U32.mul(x, k) + case 1n+p: + cl.b(p, U32.add(k, 10)) + +# b calls a in an argument too: not a tail cycle to merge +@unsafe +def nt.a(n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n+p: + nt.b(p, 1n+acc) + +@unsafe +def nt.b(+n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + Nat.add(acc, 7n) + case 1n: + nt.a(0n, Nat.add(acc, nt.a(3n, 0n))) + case 2n+p: + nt.a(p, Nat.add(acc, nt.a(Nat.mod(p, 5n), 0n))) + +# a fork in one def of the cycle +@unsafe +def fk.a(+n: Nat, acc: U32) -> U32: + match n: + case 0n: + acc + case 1n+p: + x y = fk.leaf(n) fk.leaf(p) + fk.b(p, U32.add(acc, U32.add(x, y))) + +@unsafe +def fk.b(n: Nat, acc: U32) -> U32: + match n: + case 0n: + U32.add(acc, 1) + case 1n+p: + fk.a(p, U32.mul(acc, 3)) + +def fk.leaf(+n: Nat) -> U32: + U32.from_nat(Nat.mul(n, n)) + +# a cycle the emitter may run at compile time +@unsafe +def even(n: Nat) -> U32: + match n: + case 0n: + 1 + case 1n+p: + odd(p) + +@unsafe +def odd(n: Nat) -> U32: + match n: + case 0n: + 0 + case 1n+p: + even(p) + +def tab(x: U32) -> U32: + match x: + case 0: + even(4n) + case 1: + even(7n) + case 2: + odd(7n) + case 3: + odd(2n) + case _: + even(1n) + +# the outer cycle's helper runs the inner cycle to its end each turn +@unsafe +def out.if(odd: U32, n: Nat, acc: U32) -> U32: + match odd: + case 0: + out(n, U32.add(acc, 1)) + case _: + out(n, U32.mul(acc, 2)) + +@unsafe +def out(+n: Nat, acc: U32) -> U32: + match n: + case 0n: + acc + case 1n+p: + out.if(even(n), p, acc) + +# a reusable parameter read after it is passed on, and a bound call first +@unsafe +def kp.if(hit: Bool, +s: String, n: Nat, acc: Nat) -> Nat: + match hit: + case True{}: + kp(n, s, Nat.add(acc, kp.len(s, 0n))) + case False{}: + kp(n, s, acc) + +@unsafe +def kp(+n: Nat, +s: String, acc: Nat) -> Nat: + match n: + case 0n: + Nat.add(acc, kp.len(s, 0n)) + case 1n+p: + l = kp.len(s, n) + kp.if(Nat.is_gt(l, 6n), s, p, acc) + +def kp.len(s: String, n: Nat) -> Nat: + match s: + case SNil{}: + n + case SCon{h, t}: + kp.len(t, 1n+n) + +# the helper calls a def that is not flat (it recurses in an argument) +@unsafe +def hv.if(big: Bool, +xs: List<&2, U32>, n: Nat, acc: U32) -> U32: + match big: + case True{}: + hv(n, xs, U32.add(acc, hv.sum(xs))) + case False{}: + hv(n, xs, U32.add(acc, 1)) + +@unsafe +def hv(+n: Nat, +xs: List<&2, U32>, acc: U32) -> U32: + match n: + case 0n: + acc + case 1n+p: + hv.if(Nat.is_gt(n, 3n), xs, p, acc) + +def hv.sum(xs: List<&2, U32>) -> U32: + match xs: + case Nil{}: + 0 + case h <> t: + U32.add(h, hv.sum(t)) + +# a def outside the cycle calls back into it from an argument +@unsafe +def oc.a(+n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n+p: + oc.b(p, Nat.add(acc, oc.x(p))) + +@unsafe +def oc.b(n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + 1n+acc + case 1n+p: + oc.a(p, 1n+acc) + +@unsafe +def oc.x(n: Nat) -> Nat: + match n: + case 0n: + 1n + case 1n+p: + oc.a(Nat.mod(p, 4n), 0n) + +# b calls a in an argument: every other turn holds a frame +@unsafe +def dp.a(n: Nat) -> U32: + match n: + case 0n: + 5 + case 1n+p: + dp.b(p) + +@unsafe +def dp.b(n: Nat) -> U32: + match n: + case 0n: + 9 + case 1n: + dp.a(0n) + case 2n+p: + U32.xor(1, dp.a(p)) + +# x, outside the cycle, calls back into it from an argument +@unsafe +def oq.a(n: Nat) -> U32: + match n: + case 0n: + 3 + case 1n+p: + oq.b(p) + +@unsafe +def oq.b(n: Nat) -> U32: + match n: + case 0n: + 4 + case 1n: + oq.a(0n) + case 2n+p: + oq.x(p) + +@unsafe +def oq.x(n: Nat) -> U32: + U32.xor(2, oq.a(n)) + +def deep(+n: Nat) -> U32: + a b = dp.a(n) oq.a(1n+n) + c d = dp.b(1n+n) oq.b(n) + U32.add(U32.add(a, U32.mul(b, 10)), U32.add(U32.mul(c, 100), U32.mul(d, 1000))) + +# sixteen leaves, so that some run on the pool's threads +def deep.tree(+d: Nat, +n: Nat) -> U32: + match d: + case 0n: + deep(n) + case 1n+p: + a b = deep.tree(p, n) deep.tree(p, 1n+n) + U32.add(a, b) + +def main() -> IO(Unit): + do IO: + t : Nat <- IO.now() + +z : Nat = Nat.mod(t, 1n) + +u : U32 = U32.from_nat(z) + IO.print(U32.show(ev(U32.add(27, u), 0))) + IO.print(U32.show(tree(Nat.add(6n, z), 1))) + IO.print(U32.show(tree!(Nat.add(6n, z), 1))) + IO.print(U32.show(ev!(U32.add(97, u), 0))) + IO.print(U32.show(ev.if!(True{}, U32.add(97, u), 0))) + IO.print(U32.show(both(U32.add(871, u)))) + IO.print(U32.show(apply(ev, U32.add(703, u)))) + IO.print(U32.show(apply(ev.if(False{}), U32.add(703, u)))) + IO.print(U32.show(twice(ev(U32.add(9, u)), ev.if(True{}, U32.add(9, u)), 100))) + IO.print(U32.show(later(U32.add(3, u))(24))) + IO.print(U32.show(cl.a(Nat.add(5n, z), 1)(7))) + IO.print(U32.show(cl.b(Nat.add(5n, z), 1)(7))) + IO.print(U32.show(cl.a(Nat.add(3000001n, z), 1)(7))) + IO.print(Nat.show(nt.a(Nat.add(1001n, z), 0n))) + IO.print(Nat.show(nt.b(Nat.add(1000n, z), 0n))) + IO.print(U32.show(fk.a(Nat.add(11n, z), 0))) + IO.print(U32.show(fk.b(Nat.add(12n, z), 0))) + IO.print(U32.show(U32.add(even(10n), U32.mul(odd(9n), 10)))) + IO.print(U32.show(even(Nat.add(3000001n, z)))) + IO.print(U32.show(U32.add(tab(u), U32.mul(tab(U32.add(u, 1)), 2)))) + IO.print(U32.show(U32.add(tab(U32.add(u, 2)), U32.mul(tab(U32.add(u, 3)), 2)))) + IO.print(U32.show(tab(U32.add(u, 9)))) + IO.print(U32.show(out(Nat.add(20n, z), 1))) + IO.print(Nat.show(kp(Nat.add(9n, z), String.append("abcd", Nat.show(z)), 0n))) + IO.print(U32.show(hv(Nat.add(9n, z), [1, 2, 3], 0))) + IO.print(Nat.show(oc.a(Nat.add(2001n, z), 0n))) + IO.print(Nat.show(oc.b(Nat.add(40n, z), 0n))) + IO.print(U32.show(deep.tree(4n, Nat.add(40000n, z)))) + +#|111 +#|3238 +#|3238 +#|118 +#|118 +#|361 +#|170 +#|82 +#|238 +#|114 +#|40 +#|168 +#|16500018 +#|2186 +#|2185 +#|68153 +#|68153 +#|11 +#|0 +#|1 +#|1 +#|0 +#|2047 +#|45 +#|39 +#|4502 +#|87 +#|60095 diff --git a/tests/run/tail_cycle_owners.bend b/tests/run/tail_cycle_owners.bend new file mode 100644 index 000000000..24a2f91af --- /dev/null +++ b/tests/run/tail_cycle_owners.bend @@ -0,0 +1,431 @@ +# Ownership and layouts round a tail-call cycle that runs as one loop: a list +# one def matches and its helper rebuilds, reusable lists both defs read, a +# value one def builds and the other answers, a string lent to the whole +# cycle, parameters that swap at the back edge, a helper lent a string from +# outside and handed one to own by the cycle, a record of words, an erased +# type parameter, defs whose parameters differ in count and kind, a Nat +# rebuilt as matched, an Array written and read in place, and Base's own +# cycles on text made at run time. +import Base + +# the list is matched by one def and handed on to a helper that owns it +@unsafe +def keep.if(big: Bool, h: U32, t: List, acc: List, n: Nat) -> List: + match big: + case True{}: + keep(n, t, h <> acc) + case False{}: + keep(n, t, acc) + +@unsafe +def keep(n: Nat, xs: List, acc: List) -> List: + match n: + case 0n: + acc + case 1n+m: + match xs: + case Nil{}: + acc + case h <> t: + +g = h + keep.if(U32.is_gt(g, 4), g, t, acc, m) + +# a reusable list goes round: one def reads it, the other reads it again +@unsafe +def look.if(hit: Bool, +xs: List<&2, U32>, +ys: List<&2, U32>, i: Nat, acc: U32) -> U32: + match hit: + case True{}: + look(ys, xs, i, U32.add(acc, List.sum.u(xs))) + case False{}: + look(xs, ys, i, U32.add(acc, 1)) + +@unsafe +def look(+xs: List<&2, U32>, +ys: List<&2, U32>, i: Nat, acc: U32) -> U32: + match i: + case 0n: + U32.add(acc, List.sum.u(ys)) + case 1n+j: + look.if(U32.is_gt(List.sum.u(xs), 10), xs, ys, j, acc) + +def List.sum.go(xs: List<&2, U32>, acc: U32) -> U32: + match xs: + case Nil{}: + acc + case h <> t: + List.sum.go(t, U32.add(h, acc)) + +def List.sum.u(xs: List<&2, U32>) -> U32: + List.sum.go(xs, 0) + +# one def builds the answer, the other returns it, or drops it for another +@unsafe +def mk.if(done: Bool, s: String, n: Nat) -> String: + match done: + case True{}: + s + case False{}: + mk(n, s) + +@unsafe +def mk(+n: Nat, s: String) -> String: + match n: + case 0n: + "none" + case 1n+p: + mk.if(Nat.is_eq(p, 2n), SCon{'a', s}, p) + +# a borrowed string is walked by one def, and the other decides to go on +@unsafe +def cnt.if(same: Bool, t: String, +c: Char, acc: Nat) -> Nat: + match same: + case True{}: + cnt(t, c, 1n+acc) + case False{}: + cnt(t, c, acc) + +@unsafe +def cnt(s: String, +c: Char, acc: Nat) -> Nat: + match s: + case SNil{}: + acc + case SCon{h, t}: + cnt.if(Char.is_eq(h, c), t, c, acc) + +def cnt.twice(+s: String) -> Nat: + Nat.add(cnt(s, 'a', 0n), cnt(s, 'b', 0n)) + +# parameters swap at the back edge +@unsafe +def swp.if(go: Bool, a: String, b: String, n: Nat) -> String: + match go: + case True{}: + swp(b, SCon{'x', a}, n) + case False{}: + SCon{'<', a} + +@unsafe +def swp(a: String, b: String, n: Nat) -> String: + match n: + case 0n: + swp.if(False{}, a, b, 0n) + case 1n+p: + swp.if(True{}, a, b, p) + +# the caller keeps the string: it is lent to the whole cycle +def ends(+s: String, n: Nat) -> String: + String.append(swp(s, "-", n), s) + +# the helper rebuilds the node its caller matched +@unsafe +def inc.if(odd: Bool, h: U32, t: List, acc: List) -> List: + match odd: + case True{}: + inc(t, U32.add(h, 1) <> acc) + case False{}: + inc(t, h <> acc) + +@unsafe +def inc(xs: List, acc: List) -> List: + match xs: + case Nil{}: + acc + case h <> t: + +g = h + inc.if(U32.is_eq(U32.and(g, 1), 1), g, t, acc) + +def upto(+n: Nat, acc: List) -> List: + match n: + case 0n: + acc + case 1n+p: + upto(p, U32.from_nat(n) <> acc) + +def upto2(+n: Nat, acc: List<&2, U32>) -> List<&2, U32>: + match n: + case 0n: + acc + case 1n+p: + upto2(p, U32.from_nat(n) <> acc) + +def total(xs: List, acc: U32) -> U32: + match xs: + case Nil{}: + acc + case h <> t: + total(t, U32.add(acc, h)) + +type P is Data: + P{a: U32, b: U32} + +# a record of words rides the cycle; the helper takes it apart +@unsafe +def fibp.if(more: Bool, p: P, n: Nat) -> P: + match more: + case True{}: + fibp(n, p) + case False{}: + p + +@unsafe +def fibp(+n: Nat, p: P) -> P: + match n: + case 0n: + p + case 1n+m: + P{+a, +b} = p + fibp.if(Nat.is_gt(n, 1n), P{b, U32.add(a, b)}, m) + +def P.sum(p: P) -> U32: + P{a, b} = p + U32.add(a, b) + +# an erased type parameter: the same cycle over words and over boxes +@unsafe +def rev.if(-A: Type, stop: Bool, xs: List, acc: List, n: Nat) -> List: + match stop: + case True{}: + acc + case False{}: + rev(A, n, xs, acc) + +@unsafe +def rev(-A: Type, +n: Nat, xs: List, acc: List) -> List: + match xs: + case Nil{}: + acc + case h <> t: + rev.if(A, Nat.is_eq(n, 1n), t, h <> acc, Nat.sub(n, 1n)) + +def len(-A: Type, xs: List, acc: U32) -> U32: + match xs: + case Nil{}: + acc + case h <> t: + len(A, t, U32.add(acc, 1)) + +def heads(xs: List

, acc: U32) -> U32: + match xs: + case Nil{}: + acc + case h <> t: + heads(t, U32.add(U32.mul(acc, 10), P.sum(h))) + +def nums(xs: List, acc: U32) -> U32: + match xs: + case Nil{}: + acc + case h <> t: + nums(t, U32.add(U32.mul(acc, 10), h)) + +# defs whose parameters differ in count and kind: a word where the other +# has a box, and the reverse +@unsafe +def mix.a(n: Nat, s: String, k: U32) -> String: + match n: + case 0n: + SCon{Chr{U32.add(48, k)}, s} + case 1n+p: + mix.b(U32.add(k, 1), p, SCon{'.', s}, True{}, 7) + +@unsafe +def mix.b(k: U32, n: Nat, s: String, flag: Bool, pad: U32) -> String: + match flag: + case True{}: + mix.c(s, n, U32.add(k, pad)) + case False{}: + s + +@unsafe +def mix.c(s: String, n: Nat, k: U32) -> String: + mix.a(n, SCon{'c', s}, U32.mod(k, 10)) + +# a Nat matched and rebuilt as matched in the other def +@unsafe +def dn.if(+n: Nat, big: Bool, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n+p: + match big: + case True{}: + dn(Nat.div(1n+p, 2n), 1n+acc) + case False{}: + dn(p, Nat.add(acc, 1n+p)) + +@unsafe +def dn(+n: Nat, acc: Nat) -> Nat: + dn.if(n, Nat.is_gt(n, 100n), acc) + +# an Array written in place round the cycle +@unsafe +def fill.if(odd: Bool, a: Array, +i: U32, n: Nat) -> Array: + match odd: + case True{}: + fill(n, Array.set(U32, a, i, U32.mul(i, 3)), U32.add(i, 1)) + case False{}: + fill(n, a, U32.add(i, 1)) + +@unsafe +def fill(n: Nat, a: Array, +i: U32) -> Array: + match n: + case 0n: + a + case 1n+p: + fill.if(U32.is_eq(U32.and(i, 1), 1), a, i, p) + +# the Array read is a helper's argument, and the helper hands the Array back +@unsafe +def asum.at(av: Array & U32, n: Nat, acc: U32) -> U32: + (a, v) = av + asum(n, a, U32.add(acc, v)) + +@unsafe +def asum(+n: Nat, a: Array, acc: U32) -> U32: + match n: + case 0n: + acc + case 1n+p: + asum.at(Array.get(U32, a, U32.from_nat(p)), p, acc) + +def wk.len(s: String, n: Nat) -> Nat: + match s: + case SNil{}: + n + case SCon{h, t}: + wk.len(t, 1n+n) + +@unsafe +def wk.if(more: Bool, s: String, n: Nat, acc: Nat) -> Nat: + match more: + case True{}: + wk(n, s, acc) + case False{}: + Nat.add(acc, Nat.mul(10n, wk.len(s, 0n))) + +@unsafe +def wk(+n: Nat, s: String, acc: Nat) -> Nat: + match s: + case SNil{}: + acc + case SCon{h, t}: + wk.if(Nat.is_gt(n, 0n), t, Nat.sub(n, 1n), 1n+acc) + +# wk is handed a node built for it, so it owns its string +def wk.new(+n: Nat, s: String) -> Nat: + wk(n, SCon{'q', s}, 0n) + +# the caller reads s again after the helper returns +def wk.out(+s: String, more: Bool) -> Nat: + Nat.add(wk.if(more, s, 3n, 0n), Nat.mul(1000n, wk.len(s, 0n))) + +# the helper only reads its string: a caller outside lends it one, and the +# cycle hands it one it made, which the helper must then free; gt calls itself +# too, so the helper is held in gt's loop and has no loop of its own +def rd.len(s: String, n: Nat) -> Nat: + match s: + case SNil{}: + n + case SCon{h, t}: + rd.len(t, 1n+n) + +@unsafe +def gt.if(done: Bool, +s: String, n: Nat, acc: Nat) -> Nat: + match done: + case True{}: + Nat.add(rd.len(s, acc), rd.len(s, 0n)) + case False{}: + gt(n, acc) + +@unsafe +def gt(+n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n: + gt(0n, Nat.add(acc, 50n)) + case 2n+p: + s = Nat.show(Nat.mul(n, 1000n)) + gt.if(Nat.is_eq(p, 2n), s, 1n+p, 1n+acc) + +def gt.out(+s: String, done: Bool) -> Nat: + Nat.add(gt.if(done, s, 3n, 0n), Nat.mul(100n, rd.len(s, 0n))) + +def main() -> IO(Unit): + do IO: + t : Nat <- IO.now() + +z : Nat = Nat.mod(t, 1n) + +u : U32 = U32.from_nat(z) + IO.print(U32.show(total(keep(100n, upto(Nat.add(9n, z), []), []), 0))) + IO.print(U32.show(total(keep(6n, upto(Nat.add(9n, z), []), []), 0))) + IO.print(U32.show(look(upto2(Nat.add(5n, z), []), upto2(Nat.add(3n, z), []), Nat.add(7n, z), 0))) + IO.print(mk(Nat.add(5n, z), "z")) + IO.print(mk(Nat.add(2n, z), "z")) + IO.print(Nat.show(cnt.twice(String.append("abracadabra", Nat.show(z))))) + IO.print(ends(String.append("ab", Nat.show(z)), Nat.add(3n, z))) + IO.print(swp("p", "q", Nat.add(4n, z))) + IO.print(U32.show(total(inc(upto(Nat.add(300000n, z), []), []), 0))) + IO.print(U32.show(total(inc(inc(upto(Nat.add(10n, z), []), []), []), 0))) + IO.print(U32.show(P.sum(fibp(Nat.add(30n, z), P{u, 1})))) + IO.print(U32.show(P.sum(fibp.if(True{}, P{u, 1}, Nat.add(10n, z))))) + IO.print(U32.show(nums(rev(U32, Nat.add(3n, z), [1, 2, 3, 4, 5], []), 0))) + IO.print(U32.show(nums(rev(U32, Nat.add(9n, z), [1, 2, 3, 4, 5], []), 0))) + IO.print(U32.show(heads(rev(P, Nat.add(2n, z), [P{1, 0}, P{1, 1}, P{u, 3}], []), 0))) + IO.print(U32.show(len(String, rev(String, Nat.add(2n, z), ["a", "b", "c"], []), 0))) + IO.print(mix.a(Nat.add(4n, z), "!", 0)) + IO.print(mix.b(3, Nat.add(2n, z), "!", True{}, 4)) + IO.print(mix.c("!", Nat.add(1n, z), 4)) + IO.print(Nat.show(dn(Nat.add(1000n, z), 0n))) + IO.print(U32.show(asum(Nat.add(64n, z), fill(Nat.add(64n, z), [0 : U32*64n], 0), 0))) + IO.print(Nat.show(wk(Nat.add(2n, z), String.append("abcdefg", Nat.show(z)), 0n))) + IO.print(Nat.show(wk.out(String.append("abcdefg", Nat.show(z)), True{}))) + IO.print(Nat.show(wk.out(String.append("abcdefg", Nat.show(z)), False{}))) + IO.print(Nat.show(wk.new(Nat.add(2n, z), String.append("abcdefg", Nat.show(z))))) + IO.print(Nat.show(gt(Nat.add(7n, z), 0n))) + IO.print(Nat.show(gt.out(String.append("abc", Nat.show(z)), Nat.is_eq(z, 0n)))) + IO.print(Nat.show(gt.out(String.append("abc", Nat.show(z)), Nat.is_eq(z, 1n)))) + +w : String = String.append("hello wor", String.append(Nat.show(z), "ld")) + IO.print(Bool.show(String.contains(w, "or0l"))) + IO.print(Bool.show(String.contains(w, "orl"))) + IO.print(Bool.show(String.starts_with(w, "hello w"))) + IO.print(Bool.show(String.eq(w, "hello wor0ld"))) + IO.print(String.trim_start(String.append(" \t ", w))) + IO.print(U32.show(U32.add(4000000000, U32.from_nat(z)))) + IO.print(U32.hex(U32.add(48879, U32.from_nat(z)))) + IO.print(Nat.show(Nat.add(Nat.mul(1000000n, 1000000n), z))) + +#|35 +#|11 +#|36 +#|aaaz +#|none +#|7 +#| U32: + match n: + case 0n: + acc + case 1n+p: + r1(p, U32.add(acc, 1)) + +@unsafe +def r1(n: Nat, acc: U32) -> U32: + match n: + case 0n: + U32.add(acc, 1000) + case 1n+p: + r2(p, U32.mul(acc, 3)) + +@unsafe +def r2(n: Nat, acc: U32) -> U32: + match n: + case 0n: + U32.add(acc, 2000000) + case 1n+p: + r0(p, U32.xor(acc, 5)) + +# a helper with a caller inside the cycle and one outside +@unsafe +def col.if(even: Bool, +n: Nat, steps: Nat) -> Nat: + match even: + case True{}: + col(Nat.div(n, 2n), 1n+steps) + case False{}: + col(Nat.add(Nat.mul(n, 3n), 1n), 1n+steps) + +@unsafe +def col(+n: Nat, steps: Nat) -> Nat: + match n: + case 0n: + steps + case 1n: + steps + case 1n+p: + col.if(Nat.is_eq(Nat.mod(n, 2n), 0n), n, steps) + +def col.from_even(+n: Nat) -> Nat: + col.if(True{}, n, 100n) + +# a self tail call and a cross tail call in one def; the other def is no head +@unsafe +def hop(n: Nat, acc: Nat) -> Nat: + loop(n, False{}, acc) + +@unsafe +def loop(n: Nat, b: Bool, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n+p: + match b: + case True{}: + hop(p, Nat.add(acc, 2n)) + case False{}: + loop(p, True{}, 1n+acc) + +# no head: each def calls itself and the other +@unsafe +def pa(+n: Nat, +k: Nat, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n+p: + pa.if(Nat.is_eq(Nat.mod(p, k), 0n), p, k, acc) + +@unsafe +def pa.if(sw: Bool, n: Nat, +k: Nat, acc: Nat) -> Nat: + match sw: + case True{}: + pb(n, k, 1n+acc) + case False{}: + pa(n, k, acc) + +@unsafe +def pb(+n: Nat, +k: Nat, acc: Nat) -> Nat: + match n: + case 0n: + Nat.add(acc, 1000000000n) + case 1n+p: + pb.if(Nat.is_eq(Nat.mod(p, k), 0n), p, k, acc) + +@unsafe +def pb.if(sw: Bool, n: Nat, +k: Nat, acc: Nat) -> Nat: + match sw: + case True{}: + pa(n, k, Nat.add(acc, 3n)) + case False{}: + pb(n, k, acc) + +# a def called from two places in the cycle +@unsafe +def two(n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + acc + case 1n: + two.a(0n, acc) + case 2n+p: + two.b(p, 1n+acc) + +@unsafe +def two.a(n: Nat, acc: Nat) -> Nat: + two.b(n, Nat.add(acc, 10n)) + +@unsafe +def two.b(n: Nat, acc: Nat) -> Nat: + match n: + case 0n: + Nat.add(acc, 5n) + case 1n+p: + two(p, Nat.add(acc, 100n)) + +def main() -> IO(Unit): + do IO: + t : Nat <- IO.now() + +z : Nat = Nat.mod(t, 1n) + IO.print(U32.show(r0(Nat.add(10n, z), 0))) + IO.print(U32.show(r1(Nat.add(11n, z), 7))) + IO.print(U32.show(r2(Nat.add(12n, z), 9))) + IO.print(U32.show(r0(Nat.add(3000000n, z), 0))) + IO.print(U32.show(r1(Nat.add(3000001n, z), 0))) + IO.print(U32.show(r2(Nat.add(3000002n, z), 0))) + IO.print(Nat.show(col(Nat.add(27n, z), 0n))) + IO.print(Nat.show(col.from_even(Nat.add(54n, z)))) + IO.print(Nat.show(col.if(False{}, Nat.add(27n, z), 0n))) + IO.print(Nat.show(hop(Nat.add(3000001n, z), 0n))) + IO.print(Nat.show(loop(Nat.add(3000000n, z), True{}, 0n))) + IO.print(Nat.show(pa(Nat.add(3000000n, z), 2n, 0n))) + IO.print(Nat.show(pb(Nat.add(3000000n, z), 1n, 0n))) + IO.print(Nat.show(pa(Nat.add(3000000n, z), 3000001n, 0n))) + IO.print(Nat.show(two(Nat.add(3000000n, z), 0n))) + IO.print(Nat.show(two(Nat.add(3000001n, z), 0n))) + IO.print(Nat.show(two.a(Nat.add(3000001n, z), 0n))) + IO.print(Nat.show(two.b(Nat.add(3000002n, z), 0n))) + +#|1055 +#|486 +#|2000969 +#|3431156224 +#|409817472 +#|407818478 +#|111 +#|212 +#|111 +#|4500001 +#|4500000 +#|3000000 +#|1006000000 +#|1000000001 +#|101000000 +#|101000015 +#|101000110 +#|101000115