Source/Packages

Std.Natural

packages/foundation/standard/src/Std/Natural.alpha

335 lines48 declarations13.5 KiBSHA-256 0a0af2339e6a

def · lines 230–238

naturalBelowBytePower

Full file
"value < 256^byteCount" WITHOUT ever building 256^byteCount: divide by 256 byteCount times and ask whether anything is left. A runtime natural costs about 164 bytes and 300 ns PER UNIT of its magnitude (measured 2026-09-16: 2^20 takes 0.8 s and 195 MB, 2^24 takes 5 s and 2.75 GB, and 2^32 cannot be built at all -- the image exits 125). So a bound such as 2^32 must never be materialised, and `naturalLess x (naturalPowerOfTwo 32)` is not a comparison but an out-of-memory. This is how "fits in a 32-bit field" is asked instead; it costs about value/256 steps.
230def naturalBelowBytePower =
231  (lambda unrestricted byteCount : Nat . unrestricted value : Nat .
232    naturalIsZero
233      (nat-eliminate
234        (lambda unrestricted current : Nat . Nat)
235        value
236        (lambda unrestricted predecessor : Nat . unrestricted quotient : Nat .
237          naturalDivideUnchecked quotient naturalTwoHundredFiftySix)
238        byteCount))

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.