// Record workers, edge shapes (ADR 0069): a result too wide to flatten // (stays boxed), a two-constructor type (no worker), a record passed // through a closure, a worker calling a worker, and a record built and // matched inside one function (a flat local, never allocated). import std/io type P2 { P2(x: Int, y: Int) } type P6 { P6(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) } type Tree { Leaf, Node(l: Int, r: Int) } fn sum6 :: p: P6 -> Int { match p { P6(a, b, c, d, e, f) -> a + b + c + d + e + f } } fn make6 :: x: Int -> P6 { P6(x, x + 1, x + 2, x + 3, x + 4, x + 5) } // A two-constructor type: no record worker (the tag is not static). fn size :: t: Tree -> Int { match t { Leaf -> 0, Node(l, r) -> 1 + l + r } } // The record goes through a closure call (boxed at the boundary). fn via_closure :: p: P2 f: fn(P2) -> Int -> Int { f p + f (match p { P2(x, y) -> P2(y, x) }) } // A worker calling a worker (the callee recurses: never inlined). fn swap :: p: P2 k: Int -> P2 { if k == 0 { match p { P2(x, y) -> P2(y, x) } } else { swap (match p { P2(x, y) -> P2(y, x) }) (k - 1) } } fn swap_sum :: p: P2 -> Int { let a = swap p 1 let b = swap (swap p 1) 2 match a { P2(x, y) -> x + 10 * y + match b { P2(x2, y2) -> x2 + y2 } } } // Built and matched inside one function: never allocated. fn local :: x: Int -> Int { let p = P2(x, x + 1) match p { P2(a, b) -> a * b } } pub fn main :: -> () / { io.println (show (sum6 (make6 10))) io.println (show (size (Node(size Leaf, 7)))) io.println (show (via_closure (P2(3, 4)) fn p { match p { P2(x, y) -> x - y } })) io.println (show (swap_sum (P2(5, 6)))) io.println (show (local 9)) } //> 75 //> 8 //> 0 //> 76 //> 90