// An unboxed-float worker (ADR 0045) whose loop, built unoptimised, still // calls the boxed dictionary operations: the worker reads their results // raw and must drop the boxes the boundary consumed (issue #279: two // leaked blocks per iteration, so `floats` reported 6,000,002 live blocks // at exit). `g` is the same shape at a non-tail self-call. Natively, both // ways, this ends with 0 live blocks. `add` takes one variable in two // marked owned slots (issue #283: the boundary's temps, once keyed by // variable, collided; each slot reads and drops its own). `total`'s // worker calls `bump`'s with a raw argument whose result never escapes // (issue #337): raw in, raw out, nothing boxed on the way. import std/io fn fsum :: i: Int n: Int x: Float acc: Float -> Float { if i > n { acc } else { fsum (i + 1) n (x + 0.5) (acc + x * 2.0) } } fn g :: n: Int x: Float -> Float { if n == 0 { x } else { let r = g (n - 1) (x * 2.0) if r > 0.0 { r } else { r } } } fn add :: n: Int a: Float b: Float -> Float { if n == 0 { a } else { add (n - 1) b (a + 1.0) } } // `bump` is recursive, so the simplifier keeps it, and it gets a float // worker; `total`'s worker calls it with a raw argument (a borrowed // parameter: no box, nothing to free), and the result never escapes, so // it stays raw end to end (issue #337: no box at the call, no unbox at // the use). fn bump :: x: Float n: Int -> Float { if n == 0 { x } else { bump (x + 1.0) (n - 1) } } fn total :: i: Int n: Int acc: Float -> Float { if i > n { acc } else { total (i + 1) n (acc + bump 1.5 2) } } pub fn main :: -> () / { io.println (show (fsum 0 1000 0.0 0.0)) io.println (show (g 3 0.5)) let x = fsum 0 3 0.25 1.0 io.println (show (add 3 x x)) io.println (show (total 1 1000 0.0)) } //> 500500.0 //> 4.0 //> 10.0 //> 3500.0