Certified slice extents

Use extent_at(xs, position) to copy a Plain element from [T]. Use extent_slice(xs, start, count) to borrow a subrange of [T] with count elements. Slicing supports owning elements such as String because it copies only the slice header. Both builtins require a proof during type checking, including under jihoo check. They emit the same pointer offset and load or view construction as ordinary indexing and slicing, without a bounds assertion. The usual borrow rules still track the backing storage and any loans carried by copied elements. Certified accesses can be used in functions marked #[no_panic].

Shapes and coordinates

Check the shape of flat storage in u128, then bound the coordinates in u64.

import std.extent

#[no_panic]
fn cell(xs: [i64], height: u64, width: u64, i: u64, j: u64) -> Option(i64) {
    if xs.len as u128 == (height as u128) *% (width as u128) &&
        i < height && j < width {
        let row = extent_slice(xs, i *% width, width)
        Some(extent_at(row, j))
    } else {
        None
    }
}

An exact shape guard has a widened eligible scalar or slice length on the left of ==. The right side may contain explicitly widened leaves, nonnegative integer literals, and primitive +, *, +%, or *%. The compiler checks the maximum at each node using u128: a u64 leaf can be as large as 2⁶⁴ − 1, and a slice length as large as 2⁶³ − 1. One product of two widened u64 values fits, and so does that product plus a third widened scalar. A sum of two such products can overflow. Widening a product that already ran at u64 width also cannot establish an exact shape. Chained guards can define a three-dimensional shape by first relating a widened stride to the product of two widened dimensions, then relating the length to the product of stride and the remaining dimension.

An order cut compares eligible u64 values using < or <=, or the mirrored > or >=. The lower operand can be a literal. An if condition carries true evidence from left to right through && and into the true arm. Evidence is scoped to those paths. Disjunction, negation, saved booleans, the false arm, and code after the join gain no evidence from the condition.

A primitive u64 range loop gives the fresh iteration binding the cut i < end for lo..end or i <= end for lo..=end. The saved upper bound is related to its source when that source is an eligible atom, a slice length as u64, or a u64 literal. The inclusive cut can certify an empty slice at the end when end is the slice length. A read there needs a strict bound. Loop evidence ends with the iteration body. Closures establish their own evidence using their parameters.

Recipes and stable values

A position, start, or count is a u64 recipe made of nonnegative integer literals, eligible immutable u64 bindings, eligible xs.len as u64 expressions, parentheses, and primitive wrapping +% and *%. Arbitrary calls, casts, fields, dereferences, and other operators are outside this subset. At runtime the recipe computes its natural polynomial modulo 2⁶⁴. A successful proof bounds the position plus one or the sum of start and count by the slice length. Each operand therefore fits in i64 and its wrapping evaluation yields the exact natural value.

Eligible bindings include immutable by-value parameters, immutable owned u64 locals, range iteration bindings, and a slice bound directly with let row = extent_slice(...). The compiler records that row’s length as its count recipe. A direct immutable copy let n = xs.len as u64 or let y = x records equality with its eligible source. A computed binding such as let stride = a *% b starts with its own atom. var, sink, inout, borrows and captures are ineligible. Using an atom as proof evidence also requires it to stay stable throughout the body, including after the certified access. Taking its address disqualifies it.

How the proof succeeds

Let the supply be the length of the target slice. The demand is position + 1 for a read and start + count for a slice.

  1. Expand the directed exact definitions that the supply and demand use. Collect like monomials and cancel their common coefficients on the two sides. Conflicting definitions or cycles on this expansion path fail the proof.
  2. Substitute upper bounds into the remaining demand. For x <= b, use the upper envelope of the expanded bound b. For x < b, subtract one from that envelope. A strict cut wins over a non-strict cut to the same bound. When there are distinct bounds for an atom, it stays unpromoted. Promotion cycles fail the proof. The remaining supply keeps its exact value.
  3. Subtract the demand envelope from the supply. In this signed residual, shift an atom with a positive constant lower bound by substituting that bound plus a fresh natural variable. A strict cut x < b gives b >= 1, and a literal cut k < b or k <= b can give a stronger lower bound. The proof succeeds when every coefficient after shifting is nonnegative.

This procedure has no search or runtime fallback. Its limits are depth 64, degree 16, 256 live monomials in a polynomial, 16,384 expansion and monomial-product steps, and checked i128 coefficients. A rejected access can still be written as ordinary checked indexing or slicing.

The opt-in std.extent module provides cell, row, split2, and cell3d helpers that return Option when their shape or coordinate guards can fail. Runnable example

Esc