// Record workers (ADR 0069, issue #337): a single-constructor record's // fields become C parameters at a worker's boundary. Covered: a // worker's self-recursion (flat throughout), a worker calling a worker, // an escaping record (boxed at the escape), a borrowed record // parameter, and a function whose record cell is reused (FBIP), which // keeps the boxed path. import std/io import std/list type Point { Point(x: Int, y: Int) } fn move_x :: p: Point by: Int -> Point { match p { Point(x, y) -> Point(x + by, y) } } fn norm2 :: p: Point -> Int { match p { Point(x, y) -> x * x + y * y } } fn walk :: p: Point n: Int -> Point { if n == 0 { p } else { walk (move_x p 1) (n - 1) } } // The record escapes into a list: boxed there, read back by a call. fn kept :: p: Point -> List { [p, move_x p 10] } // The cell is reused (FBIP): no record worker, the boxed path stays. fn bump :: p: Point -> Point { match p { Point(x, y) -> Point(x + 1, y + 1) } } fn bumps :: p: Point n: Int -> Point { if n == 0 { p } else { bumps (bump p) (n - 1) } } pub fn main :: -> () / { let p = Point(3, 4) io.println (show (norm2 p)) io.println (show (norm2 (walk p 1000))) let ps = kept p io.println (show (list.map ps fn q { norm2 q })) io.println (show (norm2 (bumps p 100))) } //> 25 //> 1006025 //> [25, 185] //> 21425