Open Manual

Types

Types describe the values a binding, parameter, return value, or container may hold. Start with the concrete type you expect; use ?T only when absence is a possible result, and use any at a dynamic boundary.

Contents

Everyday forms

WriteMeaningCommon use
int, str, bool, f64A concrete value type.Bindings and function signatures.
list<T>, dict<K, V>, set<T>A typed collection.Data kept within Nytrix.
?TT or nil.Optional input or lookup result.
T<A>A generic type.Option<int>, Result<T, E>.
struct NameA Nytrix record value.Ordinary program data.
enum NameA finite set of variants.State and alternatives.
layout NameAn ABI-shaped record.FFI and raw memory only.
layout Pixel {
   u8 r,
   u8 g,
   u8 b,
   u8 a
}

Rules

  1. Prefer a concrete type. Use ?T when nil is a valid result and any at a dynamic boundary.
  2. Use struct for ordinary records and layout when field order and representation are part of an ABI. Pointers, handles, and fnptr remain distinct types.
  3. A refinement keeps its base representation and adds a compile-time predicate; construction or calls require the compiler to prove that predicate.
  4. Fin<N> requires a positive compile-time bound and proves 0 <= value < N. proof<P> is erased compile-time evidence, not a runtime boolean.

Do not use a handle as a pointer unless the foreign API documents that conversion.

See Native for headers, ownership, strings, packing, and alignment.

Proof and refinement types

User-defined refinements name a base type plus a compile-time predicate over the value:

use core

refine Even = int where (v) { v % 2 == 0 }
refine Positive = int where (v) { v > 0 }
refine PositiveEven = Even where (v) { Positive(v) }

def PositiveEven n = 4
assert(n == 4, "positive even binding")
assert(Even(6), "even membership")

Membership tests evaluate the whole intersection: PositiveEven(6) is true, while PositiveEven(5) and PositiveEven(-4) are false.

refine X = Base where (v) { pred } introduces a new nominal type X whose representation is exactly Base (ABI-preserving), carrying the predicate as a compile-time obligation on construction. A refinement of a refinement substitutes the inner bindings into the outer predicate, forming an intersection (PositiveEven is both > 0 and even). Comptime-only predicates use the bracketed form exists(k) { pred }:

refine EvensUpToPowerTwo = int where (v) { exists(k) { v == 2 * k } }

The proof kernel discharges refinement predicates at compile time: constant initializers and call arguments are verified against the predicate via compile-time evaluation or the Presburger/Fourier-Motzkin solver. Refinements are subtyped covariantly (Even <: int), preserving the underlying ABI, and membership can be evaluated as a compile-time value (assert(Even(6))).

Fin<N> is the bounded integer type whose values satisfy 0 <= value < N. N must normalize to a positive compile-time integer. It uses the integer ABI, so passing a Fin<N> to an int parameter is representation-preserving, but constructing or passing an integer as Fin<N> requires a proven bound.

use core

fn load_slot(Fin<4> index) int {
   def values = [10, 20, 30, 40]
   values[index]
}

def Fin<4> selected = 2
assert(load_slot(selected) == 30, "bounded index")

Different bounds remain different static types: Fin<4> is not silently reinterpreted as Fin<8>. Literal and immutable compile-time bounds may be used in generic specialization keys. Functions may specify parameter-dependent requires <expr> preconditions and decreases(expr) termination measures for recursive descent.

User-defined indexed types such as Slot<N> require a declared Slot base type; the index does not synthesize an undeclared type. The indexed form keeps the declared base ABI, while integer literals passed to Slot<N> are checked against 0 <= value < N. For that check, a symbolic N must resolve to an immutable positive compile-time integer.

proof<P> is erased compile-time evidence that proposition P was proved. It is not a runtime boolean and ordinary values cannot stand in for it.

use core

fn require_positive(proof<5 > 0> evidence) int {
   5
}

def proof positive = prove(5 > 0, "positive constant")
require_positive(positive)

fn lemma declares a named proposition for use with prove. A lemma body is one proposition and may use A → B implication syntax:

use core

fn lemma positive_sum(int x, int y) {
  x > 0 && x < 100 && y > 0 && y < 100 → x + y > 0
}

def proof sum_is_positive = prove(positive_sum(3, 4))

The application is checked at compile time and the resulting witness is erased, like any other proof<P> value.

Refinement interfaces

--emit-refinement-interface=PATH writes a versioned interface after successful type validation; --use-refinement-interface=PATH imports it before type validation. Interface records preserve the refinement's qualified name, base type, formal name, and canonical predicate. The format is tied to the compiler semantic version and rejects malformed, unsupported, or incompatible records instead of importing an empty interface.

The interface carries only predicates accepted by the canonical pure proposition encoding. Unsupported expressions, explicit literal type hints, and strings with embedded NUL bytes fail export. The importer accepts scalar literal/formal boolean and comparison expressions; it does not transport mutable state or aggregate alias facts. Imported refinements participate in the same guarded-path checks as source declarations.

Proposition matching is structural: equivalent equality/order spellings are normalized, while unrelated propositions are rejected. prove accepts only a condition the compiler can establish; false and unknown conditions fail.

This section describes the refinement-proof subsystem, not the complete set of dependent-type features; see Dependent types and indexed data for Pi/Sigma types, GADTs, and type-level forms. Nytrix does not implement arbitrary liquid-type or refinement-qualifier synthesis: nullable, range, index, and discriminant facts in the compiler's refined.v1 artifact are heuristic flow facts, not synthesized user refinements. Calls transport only supported caller facts into parameter-dependent preconditions. A mutation preserves a refinement only when the analysis proves it remains valid; uncertain aliasing does not count as proof. Unsupported obligations remain errors or explicit dynamic checks, never implicit evidence.

Proof boundary regression matrix

The acceptance boundary is exercised by paired positive and negative cases:

BoundaryAccepted caseRejected or inconclusive case
Closed and normalized propositionsproof-indexed.nshape, arithmetic-normalization.nshapeproof-solver-soundness-negative.nshape, proof-proposition-mismatch.nshape
Bounded nonlinear intervals and finite splittingpolicy-certificates.nshapeSame fixture checks a false bound, unsafe overflow premise, and unproved nonlinear obligation
Resource and parser limitsSame fixture checks an over-budget canonical proposition stays unknownpolicy-recursion.nshape checks depth-limit rejection and fail-closed certificates
Refinement construction and transportrefinements.nshape, refinement-interface.nshapetype-errors.nshape, relational-refinement-guard.nshape cover unknown, mutation, and closure boundaries
Caller obligations across execution routesportable-contracts.nshape, portable-contract-binding-capacity.nshapeportable-contracts-errors.nshape, proof-call-site-unknown-negative.nshape

These fixtures cover the supported calculus and its fail-closed edges. Function preconditions and termination measures are described in Functions; trait laws are described in Traits and impl. See Comptime for compile-time assertions and proof construction rules.