// `Int` outside 63 bits becomes a big integer instead of overflowing // (ADR 0014). Arithmetic, comparisons, `show`, `hash`, parsing and // sending agree on both backends, and results that fit are small again. import std/array import std/bytes import std/io import std/list import std/process.{type Subject, type proc} import std/string fn factorial :: n: Int -> Int { if n <= 1 { 1 } else { n * factorial (n - 1) } } fn power :: b: Int e: Int -> Int { if e == 0 { 1 } else { b * power b (e - 1) } } pub fn main :: -> () / { boundaries () big_values () comparisons () conversions () sending () } fn boundaries :: -> () / { let max = 4611686018427387903 let min = -4611686018427387904 io.println ( show (max + 1) <> " " <> show (min - 1) <> " " <> show (max * 2) <> " " <> show (min * -1) <> " " <> show (min / -1) <> " " <> show (-min) ) io.println ( show (max + 1 - 1 == max) <> " " <> show (min - 1 + 1 == min) <> " " <> show (max * 2 / 2) <> " " <> show (max + max - max) ) // Back in range, a result is small again: it matches a literal. match max * 4 / 4 { 4611686018427387903 -> io.println "small again" , _ -> io.println "still big" } } fn big_values :: -> () / { io.println (show (factorial 100)) let p = power 2 100 io.println ( show p <> " " <> show (p % 1000) <> " " <> show (factorial 100 / factorial 98) ) io.println ( show (-p / 7) <> " " <> show (-p % 7) <> " " <> show (p * p - p * p + 5) <> " " <> show (p + 3 - p) <> " " <> show (p / p) ) io.println ( show (power 2 64) <> " " <> show (power 2 64 - 1) <> " " <> show (-power 2 64) ) io.println (show (list.fold (list.range 1 10) 0 fn acc i { acc + i * p })) } fn comparisons :: -> () / { let p = power 2 70 let q = power 2 35 * power 2 35 io.println ( show (p == q) <> " " <> show (p == q + 1) <> " " <> show (p != 5) <> " " <> show (hash p == hash q) <> " " <> show (hash (-p) == hash p) ) io.println ( show (p > 5) <> " " <> show (-p < -4611686018427387904) <> " " <> show (p < p + 1) <> " " <> show (-p - 1 < -q) <> " " <> show (p >= q) ) io.println (debug [p, -p, 1] <> " " <> show (list.map [p, 7] fn x { x % 10 })) } fn conversions :: -> () / { io.println ( show (string.to_int "123456789012345678901234567890") <> " " <> show (string.to_int "-99999999999999999999") <> " " <> show (string.to_int "+5") <> " " <> show (string.to_int "1_000") <> " " <> show (string.to_int "") ) // Indices and bounds that are big: out of range, or clamped. let huge = power 2 70 let a = array.from_list [1, 2, 3] io.println ( show (array.get a huge) <> " " <> show (array.get a (-huge)) <> " " <> show (array.slice a (-huge) huge) <> " " <> show (bytes.get (bytes.from_string "ab") huge) ) } fn doubler :: inbox: Subject<(Int, Subject)> left: Int -> () / { if left > 0 { let (n, reply) = process.receive_forever inbox process.send reply (n * 2) doubler inbox (left - 1) } else { () } } // Big integers are values like any other: sent, kept, and sent back. fn sending :: -> () / { let inbox = process.subject () let reply = process.subject () let _ = process.spawn fn { let mine = process.subject () process.send inbox mine doubler mine 2 } let worker = process.receive_forever inbox let p = power 3 50 process.send worker (p, reply) io.println (show (process.receive_forever reply) <> " " <> show p) process.send worker (power 2 61, reply) io.println (show (process.receive_forever reply)) } //> 4611686018427387904 -4611686018427387905 9223372036854775806 4611686018427387904 4611686018427387904 4611686018427387904 //> True True 4611686018427387903 4611686018427387903 //> small again //> 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 //> 1267650600228229401496703205376 376 9900 //> -181092942889747057356671886482 -2 5 3 1 //> 18446744073709551616 18446744073709551615 -18446744073709551616 //> 69720783012552617082318676295680 //> True False True True False //> True True True True True //> [1180591620717411303424, -1180591620717411303424, 1] [4, 7] //> Ok(123456789012345678901234567890) Ok(-99999999999999999999) Ok(5) Error(not an integer: "1_000") Error(not an integer: "") //> None None [1, 2, 3] None //> 1435795975383705177540498 717897987691852588770249 //> 4611686018427387904