Refinement Types
Invariants on scalar types, composition, construction, patterns, assignment, and runtime checks.
Contents
A refinement type has the same representation as a base type plus one or more invariants. The invariant is a boolean predicate over self. A value of the refinement type is guaranteed to satisfy it.
Declaring a Refinement#
A refinement uses = to name the base type and one or more : invariant clauses.
type Positive = i32 : invariant self > 0
type Percent = i32
: invariant self >= 0
: invariant self <= 100
The base must be a scalar or pointer type. Records, sums, enums, Option, Result, functions, generic references, and type parameters cannot be refinement bases.
A refinement of a refinement composes the invariants. The new type inherits every invariant of its base and appends its own.
type Positive = i32 : invariant self > 0
type Percent = Positive : invariant self <= 100
A Percent must be positive and at most one hundred. Construction checks the whole chain.
Invariant Expressions#
An invariant must be boolean. It may use arithmetic, comparisons, bitwise operators, module constants, and calls to pure functions. It may not call an impure function.
pure fn even(n i32) bool => n % 2 == 0
type Even = i32 : invariant even(self)
const LIMIT i32 = 100
type Small = i32 : invariant self > 0 && self < LIMIT
Preconditions and postconditions may call impure helpers, at the cost of runtime evaluation. A type invariant cannot.
Construction#
Normal construction has the refinement type and must never fail. It is allowed only when the input is a compile-time constant that satisfies the invariants, or when the value already has the refinement type. A constant that violates an invariant is a compile error.
const good = Positive { 5 } // accepted and checked at compile time
const bad = Positive { 0 } // compile-time error
A refinement literal uses a single positional value.
const raw = 42
const value = Positive { raw } // error: raw is not a compile-time constant
For runtime values, use checked construction with ?. The enclosing function must return Result[_, str] when a runtime check is needed. On failure, the error is "invariant failed for 'Positive'".
fn make_positive(n i32) Result[Positive, str] => Success(Positive { n }?)
If the compiler can prove the construction, ? is redundant and produces a warning. If it can prove the construction invalid, it is a compile error even with ?. See Checked Construction.
Assignment#
A refinement value can be assigned to its base type without a check. The reverse requires construction.
fn as_i32(value Positive) i32 => value
fn from_i32(n i32) Result[Positive, str] => Success(Positive { n }?)
Arithmetic on a refinement produces the base type. value + 1 is an i32, not a Positive. A cast or truncation cannot target a refinement; use checked construction.
fn increment(value Positive) Result[Positive, str] {
const next = value + 1
return Success(Positive { next }?)
}
Patterns#
is tests a value against a refinement and binds the refined value when the invariant holds.
const raw = 42
if raw is Positive(p) {
print(p)
} else {
print("not positive")
}
The bound name has the refinement type. The test evaluates the invariant at runtime. A refinement pattern takes at most one binding.
match does not accept a refinement pattern. Use is for refinements and match for sum-shaped types.
Records#
A record field may have a refinement type. Construction checks the field invariant.
type Positive = i32 : invariant self > 0
type Counter {
n Positive
}
type Window : invariant lo <= hi {
lo Positive
hi Positive
}
fn bump(counter Counter) Result[Counter, str] {
counter.n = Positive { counter.n + 1 }?
return Success(counter)
}
Counter has no cross-field invariant, so counter.n may be assigned when the new value is a Positive. Window must be replaced as a whole.
fn shift(window Window, by i32) Result[Window, str] {
const lo = Positive { window.lo + by }?
const hi = Positive { window.hi + by }?
return Success(Window { lo = lo, hi = hi }?)
}
Construction-Time Checks#
A refinement invariant is checked at construction. It is not re-checked on reads, and the language does not maintain the invariant after mutation. Compound assignment on a refinement is rejected, and arithmetic produces the base type. Treat a refinement as immutable; construct a new value when it must change.
Composition#
pure fn positive(n i32) bool => n > 0
pure fn ordered(lo i32, hi i32) bool => lo <= hi
type Positive = i32 : invariant positive(self)
type Percent = Positive : invariant self <= 100
type Window : invariant ordered(lo, hi) {
lo Positive
hi Percent
}
Constructing a Window checks each field's refinement and then the cross-field relation.
Traits#
A refinement is a distinct trait target. An implementation for the refinement does not apply to the base, and an implementation for the base does not apply to the refinement.
trait Ord {
fn lt(x, y Self) bool
}
type Positive = i64 : invariant self > 0
impl Positive of Ord {
fn lt(x, y Self) bool => x < y
}
Design#
Use a refinement for a simple predicate over one value: a positive count, a percentage, an aligned pointer. Use a record invariant when the constraint relates several fields. Use a sum type when the value is one of several alternatives.
Prefer small, pure invariants that are cheap to evaluate. Complex invariants make construction expensive and compile-time evaluation less likely.