// Unboxed-int workers (ADR 0045, Revision 2): an Int worker computes on // raw int64s and bails to the boxed wrapper — which recomputes the call // with big-integer promotion — when a result leaves the 63-bit small // range. Both back ends must agree, whether the worker bails or not. import std/io fn fact :: n: Int -> Int { if n <= 1 { 1 } else { n * fact (n - 1) } } fn power :: b: Int e: Int -> Int { if e == 0 { 1 } else { b * power b (e - 1) } } // Tail-recursive: the same bail from a loop-shaped worker. fn muls :: n: Int acc: Int -> Int { if n == 0 { acc } else { muls (n - 1) (acc * 2 + 1) } } // A big-integer argument takes the boxed call at the boundary. fn bump :: n: Int -> Int { n + 1 } // -2^62 / -1 leaves the small range: the worker's division bails and the // recompute promotes (and the promoted argument re-enters boxed). fn steps :: n: Int -> Int { if n >= 0 { n } else { steps (n / (0 - 1)) } } // Remainder and division in a worker, on a promoted (big) value too. fn digits :: n: Int -> Int { if n < 10 { n } else { n % 10 + digits (n / 10) } } pub fn main :: -> () / { // fact(20) fits a small int (no bail); fact(25) promotes mid-recursion. io.println (show (fact 20) <> " " <> show (fact 25)) io.println (show (power 2 63) <> " " <> show (power (0 - 2) 63)) io.println (show (muls 70 0) <> " " <> show (muls 40 0)) io.println (show (bump 41) <> " " <> show (bump (power 2 70))) io.println (show (steps 5) <> " " <> show (steps (0 - 4611686018427387903 - 1))) io.println (show (digits (fact 30)) <> " " <> show (digits 998877)) } //> 2432902008176640000 15511210043330985984000000 //> 9223372036854775808 -9223372036854775808 //> 1180591620717411303423 1099511627775 //> 42 1180591620717411303425 //> 5 4611686018427387904 //> 117 48