I just created this draft PR to introduce partition_point function on List, which is basically the binary search algorithm. I need some help with the following:
List? The other languages I know come with partition_point and binary_search. I really dislike the latter. In C++ it returns boolean, in Rust a Result of the index (any of the valid ones) or could-have-been-index. I dislike these two versions of binary_search for the same reason I like std::lower/upper_bound in C++. These are actually useful. Any thoughts on also adding some version of List.binary_search?Does this function belong in
List?
I don't love the "assuming list is partitioned under predicate", it seem more idiomatic to have a type that would enforce this.
can it be future-proofed with some tooling, in case the TCO requirements or the implementation itself change?
If we include this in my upcoming perf regression check, that would do the job.
We've discussed something like a TCO annotation in the past, but it's not a high priority for now.
How can I check if my implementation does:
From Opus 5.5:
The practical check is a stress test: call the function with an input big enough to make it recurse millions of times.
1. Stress test with
expectsum_tail : U64, U64 -> U64 sum_tail = |n, acc| if n == 0 acc else sum_tail(n - 1, acc + n) expect sum_tail(10_000_000, 0) == 50_000_005_000_000Run it with
roc test MyFile.roc --no-cache. I tried this with your localroc-mainbuild:
sum_tail(tail call): the test passed in about 0.35 s.n + sum_plain(n - 1)(not a tail call): at the same depth it fails with:
This Roc program overflowed its stack memory. This usually means there is very deep or infinite recursion somewhere in the code.If your function survives a depth in the millions, it qualifies. This test doesn't tell you which optimization kicked in, but that rarely matters: each one means the stack doesn't grow.
2. What makes a call qualify
From the "Tail Calls" section of
docs/langref/functions.mdand the compiler code (src/lir/tail_call_builder.zig), the recursive call's result has to be returned exactly as it is.These still qualify:
- The call sits at the end of an
iformatchbranch.- You bind the result to a name and return that name.
- An early
return f(x).These don't qualify:
- Doing anything with the result after the call:
n + f(x),f(x)?,Ok(f(x)), calling a function on it, and so on.- The usual fix is to pass an accumulator along as an extra argument, like
sum_tailabove.Exception, "modulo cons": if the function returns a recursive tag union and the call's result goes straight into one of its tags, it also becomes a loop. For example,
Cons(x, count_up(n + 1, end))where the return type isLinkedList(a).Calls to other functions: a tail call to a different function, or to a function passed in as an argument, reuses the current stack frame. So mutually recursive functions like
is_even/is_oddthat only call each other in tail position can't overflow either.3. Compiler debug flag
roccan be built with a debug option that logs this:zig build roc -Dprint-trmc=true # one line per transformed function on stderr zig build roc -Dprint-ir-after-trmc=true # full IR dump of each transformed functionThe output looks like
tce: proc p12 (0 construct sites, 1 tail calls). It identifies functions by internal number, not by name, so it's most useful on a small file with a single recursive function. I haven't tried these flags; the stress test is simpler.
That was quite helpful @Anton . I ran some experiments and found a version of the implementation that enables TCO. Here it is:
partition_point : List(a), (a -> Bool) -> U64
partition_point = |list, predicate| {
partition_point_helper : List(a), (a -> Bool), U64 -> U64
partition_point_helper = |lst, pred, offset| {
if lst.is_empty() {
return offset
}
mid_idx = lst.len() / 2
mid = list_get_unsafe(lst, mid_idx)
if predicate(mid) {
partition_point_helper(lst.drop_first(mid_idx + 1), pred, offset + mid_idx + 1)
} else {
partition_point_helper(lst.take_first(mid_idx), pred, offset)
}
}
partition_point_helper(list, predicate, 0)
}
The PR is no longer a draft, ready for review.
Thanks for your work on this @Vanand Gasparyan, I am going to come back to this when I can.
Last updated: Nov 26 2026 at 20:35 UTC