Skip to Content
Language in depth

Types, widths and proofs

Four number types, range types from the statement, storage widths, Euclidean division, Mod<P>, and what the compiler proves from them.

A C++ solution picks int or long long per variable and gets it wrong in one of two ways: int where a product overflows, or long long arrays that hit the memory limit. CP Lang separates the two questions. Values compute in 64 bits; storage takes the fewest bytes the type allows, and the type that decides is the constraint you copy from the statement anyway. The normative rules are numbers and layout §1 and values and types.

Four number types and range types

There are four number types: Int (64-bit, symmetric), U64 (wrapping, for hashes and bit tricks), I128 (for products past 64 bits) and F64. There is no I32, U32 or U8. A narrow slot is a range type instead: 0..=1e9, 1..=n, 0..7.

A range type is a contract. Reading into one validates the input, and storing into one checks the value in every checked build:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

The read stops at the value that broke the statement's bound, naming the line and column, so a misread format shows up on the first sample instead of as a wrong answer.

Where widths come from

WrittenStored as
Int8 bytes
lo..=hi with static endsthe narrowest of 1, 2, 4 or 8 bytes that holds it
a domain position Vthe narrowest that holds V.count + 1 values
Mod<P>4 bytes when P < 2^31
Bool, Char1 byte

So [V]0..=1e9 is 4 bytes per element and [V]V parents with n: 1..=2e5 are 4 bytes each, with no annotation. Locals never narrow: a let x: 0..=1e9 lives in a 64-bit register, where width is free. Loads widen, stores check: reading a[i] from a [V]0..=1e9 gives an Int, so a[i] * a[j] can't overflow 32 bits, and writing back into the array checks the range unless it's proved.

What the compiler proves

Proofs are interval arithmetic on the ranges you declared, within one function. A store the proof covers costs nothing. a[v] % 7 is always in 0..7, because % is Euclidean (below):

Output
Press Run (Ctrl+Enter).

A store the proof doesn't cover is checked. a[v] + 1 can be 1e9 + 1, and the checked build stops on the element that doesn't fit, with the local values:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

The same proofs remove overflow checks: 0..=1e9 times 0..=1e9 provably fits in Int, so it compiles to a plain multiplication. When the proof shows an expression fits in 32 bits, the C++ computes it in 32 bits, which never changes the answer and doubles the lanes when a loop vectorizes. What each build does with an unproven check is the checked operations chapter.

Division and modular numbers

Integer division is Euclidean: a % b is never negative, for any sign of a or b, and a == (a / b) * b + a % b. Where the compiler proves a >= 0 and b > 0, it emits C++'s native / and % with no extra code.

Output
Press Run (Ctrl+Enter).

C++ prints -1 -2 here. A modulus is a type, Mod<P>, so every operation on it reduces, and a missing % MOD can't be written:

Output
Press Run (Ctrl+Enter).

Why it's designed this way

Width from the types you already write. decision 149 adopts the numbers and layout chapter: the statement's constraints, written once as types, give storage widths, input validation and overflow proofs at the same time. Choosing a width is automatic only because the choice never loses: same operations, never more bytes, never slower. The rejected alternative, narrowing locals, saves nothing in a register and costs conversions.

No second integer family. Rust and C++ offer i32, u8 and the rest as types of their own. Decision 1.7 keeps four number types instead: a range already says how many bytes a slot needs, so I32 and U8 would only add casts and a second, narrower place to overflow.

Range stores are checked. decision 100 made every store into a range-typed place go through one check. Before it, a narrowed field silently truncated in C++ while cpl run printed the full value, so the interpreter and the judge disagreed. Now the checked build traps, and valid programs pay nothing on the judge.

Euclidean / and %. C++'s truncating % gives negative remainders for negative inputs, the source of the ((a % m) + m) % m idiom and of the bugs where it was forgotten. Euclidean division removes the idiom, and the proof-based lowering keeps the common non-negative case at C++ speed (decision 1.9).

On this page