Skip to content

ADR 0005: The scalar builtin-function surface

Status: Final Type: Feature Created: 2026-07-27 Amended-By: 0020 (pow(x, y) with a negative exponent), 0075 (an int past 64 bits, parsed and returned) Related-ADRs: 0003 (script mode), 0004 (Str values and the shared formatting runtime), 0028 (min, max and key= over an iterable). Provenance: arbitrations A1–A3, A143, A179.

Context

With script mode and Str values landed, the largest cluster of real-program surface the compiler could close without a new value representation was the builtin functions whose arguments and results are scalars or Str. This ADR fixes what that surface is and what it refuses.

Decision

  1. In: the builtins that need no aggregate. min/max over two or more scalar arguments; pow(x, y) and pow(x, y, mod); divmod(a, b); chr, ord, bin, hex, oct; repr of a number or bool; int(s), int(s, base), float(s); the zero-argument constructors int(), float(), bool(), str(); abs, round(x), round(x, n) and len of a Str. min and max over an iterable, with key=, are 0028's.

  2. min/max return one of their arguments, so their arguments must share a type. CPython's min is result = a0 then if x < result: result = x, keeping the first minimal argument (max uses >); the lowering is that exact fold, which is what makes ties and NaN come out the same way. It follows that min(1, 2.5) is the int 1 and prints 1. The result type is not known until run time. Promoting to Float64 would print 1.0, and promoting min(True, 2) to Int64 would print 1 where CPython prints True. Both are refused (PP900).

  3. pow(x, y) is rewritten to x ** y, so the two spellings cannot drift apart; the overflow check comes with it, and so does 0020's rule for a negative exponent. pow(x, y, mod) is one runtime helper, exact for any modulus (a 128-bit product for a 64-bit one, the big-int runtime past it, 0075). It follows CPython's negative exponent (the modular inverse, 3.8+) and raises both of its ValueErrors in generated code: one for a zero modulus, one for a base with no inverse. The helper reports the second by returning the modulus itself, a value no reduction produces. divmod(a, b) is the tuple (a // b, a % b) wherever a tuple lowers, so the operators' own sign rules and zero-divisor raise apply.

  4. round is CPython's. round(x) of a float is rint converted as int(...) converts, so it is an int (A143); round(n) and round(n, k) with k >= 0 are n; round(n, -k) rounds half to even at 10**k in checked arithmetic. round(x, n) of a float stays a float of x's width; a negative n rounds left of the point through %.*e, which is correctly rounded at a number of significant digits as %.*f is at a number of places. A NaN or an infinity raises CPython's ValueError or OverflowError (A191).

  5. Formatting and parsing are CPython's, exactly, in the shared runtime. bin/hex/oct spell the sign before the prefix and use lowercase digits (hex(-255) is -0xff, bin(0) is 0b0). int(s[, base]) and float(s) implement Python's grammar: surrounding whitespace, an optional sign, base prefixes (0x/0o/0b, and base 0 inferring from them), and a single underscore strictly between two digits. The hex significand (0x1p3) and the nan payload, which strtod accepts and Python's float() does not, are rejected before parsing. repr of a number or bool is str of it (Python 3 gives them the same text). int(s) of a number past 64 bits is CPython's value where int grows (0075); with the 64-bit int it stops the program, naming the overflow.

  6. Where CPython raises, the compiled program raises the same exception, which a handler catches. int(s) and float(s) of a string either parser rejects raise CPython's ValueError from generated code, with CPython's message: invalid literal for int() with base 10: 'abc' (the repr cut at 200 characters, as CPython's %.200R does), could not convert string to float: 'abc', and int() base must be >= 2 and <= 36, or 0. The runtime reports a status before the parse (__pp_str_i64_status, __pp_str_f64_status) and one runtime call builds the message (__pp_str_parse_error): a repr, a slice and concatenations in generated code would leave each owned temporary as a stack slot live across its function. chr(n) outside range(0x110000) raises chr() arg not in range(0x110000), past 64 bits or not; an argument that is a constant in range emits no test. Returning a nearby value is the failure mode this project does not have, and stopping where CPython raises is distance (A179). Three cases still stop the program, naming the fault and the source line: ord of a str whose length is not one, the repr of a non-ASCII literal in a parse error's message (A12), and the parse of a number past 64 bits under the 64-bit int.

  7. repr of a str is CPython's on the ASCII domain (0028): exact inside it. The first byte past it stops the program, since which characters CPython leaves bare inside the quotes follows the Unicode printability tables.

  8. One runtime source, three backends. All of it lives in backends/runtime.py as a single {q}-parameterised C unit: the C backend inlines static copies, QBE and LLVM link the extern copy as a RuntimeUnit gated by module_uses_builtins(). No per-backend copy of any helper, so the three are bit-identical by construction. The CPython differential runs per backend.

Consequences

The scalar builtin surface compiles identically on all three backends and matches CPython on everything it accepts. Everything outside it refuses by name with one diagnostic that names the blocker. That is the point of an enumerated surface even for the programs it does not yet compile.