prose: infix + paren elision + doc-wrap + nested-match (iter 20b)

Eleven binary builtins (+ - * / % == != < <= > >=) render as
lhs op rhs when called with two args (tail-flagged keeps prefix).
Standard Rust-aligned 4-level precedence table elides parens.
not(x) renders as !x. Long /// doc strings wrap at 80 cols on
word boundaries.

bench_list_sum: 'if ==(n, 0)' becomes 'if n == 0', '+(acc, h)'
becomes 'acc + h', tail keyword still visible on tail calls.

19 new unit tests (47 total). 4 snapshots (3 re-rendered + 1 new
bench fixture). Public API unchanged.
This commit is contained in:
2026-05-08 18:13:43 +02:00
parent a9d57c5c81
commit 9cf0e3e81c
5 changed files with 645 additions and 9 deletions
+41
View File
@@ -0,0 +1,41 @@
// module bench_list_sum
data IntList = INil | ICons(Int, IntList)
/// Tail-recursive list builder. Result = accumulator-prepended list.
fn cons_n_acc(n: Int, acc: IntList) -> IntList {
if n == 0 {
acc
} else {
tail cons_n_acc(n - 1, ICons(n - 1, acc))
}
}
/// Build [0, 1, ..., n-1] :: IntList. Order doesn't matter for sum.
fn cons_n(n: Int) -> IntList {
cons_n_acc(n, INil)
}
/// Tail-recursive sum.
fn sum_acc(xs: IntList, acc: Int) -> Int {
match xs {
INil => acc,
ICons(h, t) => tail sum_acc(t, acc + h)
}
}
/// Sum every element. Calls sum_acc with seed 0.
fn sum_list(xs: IntList) -> Int {
sum_acc(xs, 0)
}
/// Build a list of length n, sum it, print the sum.
fn run_one(n: Int) -> Unit with IO {
do io/print_int(sum_list(cons_n(n)))
}
fn main() -> Unit with IO {
run_one(100000);
run_one(1000000);
run_one(3000000)
}
+5 -2
View File
@@ -3,7 +3,9 @@
/// Recursive Int list — boxed.
data IntList = Nil | Cons(Int, IntList)
/// Take ownership of an IntList; return its head if Cons, else 0. The tail is loaded as a pattern binder but never consumed — iter A dec's it at arm close. The outer cell is dec'd at fn return via iter B's Own-param emission.
/// Take ownership of an IntList; return its head if Cons, else 0. The tail is
/// loaded as a pattern binder but never consumed — iter A dec's it at arm
/// close. The outer cell is dec'd at fn return via iter B's Own-param emission.
fn head_or_zero(xs: own IntList) -> Int {
match xs {
Nil => 0,
@@ -11,7 +13,8 @@ fn head_or_zero(xs: own IntList) -> Int {
}
}
/// Build a 5-element IntList; pass to head_or_zero (transferring ownership); print the result.
/// Build a 5-element IntList; pass to head_or_zero (transferring ownership);
/// print the result.
fn main() -> Unit with IO {
let xs = Cons(11, Cons(22, Cons(33, Cons(44, Cons(55, Nil)))));
do io/print_int(head_or_zero(xs))