Std.Bounded (Cure Standard Library v0.37.2)

View Source

Bounded natural indices.

Bounded(n) is inhabited by values strictly less than n. It is the stdlib replacement for the traditional Fin(n) name and is used for length-safe indexing into Std.Vector.

Examples

use Std.Bounded
use Std.Nat

let idx = assert_type First() : Bounded(S(Z()))
let right = inject_right(S(Z()), idx)
split_sum(S(Z()), right)

Summary

Types

Structural view of a value in a disjoint n + m state space. This is the elimination counterpart to inject_left/inject_right: callers learn which side owns the value and recover a value with that side's exact bound, without converting through an unchecked Nat.

Functions

Wrap a left-side bounded value in BoundedSum with explicit erased bounds.

Wrap a right-side bounded value in BoundedSum with explicit erased bounds.

Embed the left side of a disjoint n + m state space.

Embed a value from the left bound into a disjoint left + right space with explicit erased bounds.

Embed the right side of a disjoint n + m state space. Each constructor in the left bound shifts the right value once, making the two images disjoint by construction.

Embed a value from the right bound into a disjoint left + right space with an explicit erased right bound.

Increment the left bound of a BoundedSum, shifting left values by Next and leaving right values unchanged.

The structural splitter is a left inverse of both disjoint injections. These equations are the elimination laws needed by dependent consumers: once a combined state is already known to come from one side, matching the proof reduces split_sum to that exact side and value.

Split a bounded value in left + right into either BoundedLeft or BoundedRight.

Preserve a bounded value while extending its upper bound on the right. The recursion follows the value itself, so no unchecked conversion from Nat is involved.

Types

type Bounded indices (n: Nat)

 type Bounded indices (n: Nat)
   First : Bounded(S(m))
   Next : Bounded(m) -> Bounded(S(m))

type BoundedSum indices (left: Nat, right: Nat)

 type BoundedSum indices (left: Nat, right: Nat)
   BoundedLeft : Bounded(left) -> BoundedSum(left, right)
   BoundedRight : Bounded(right) -> BoundedSum(left, right)

Structural view of a value in a disjoint n + m state space. This is the elimination counterpart to inject_left/inject_right: callers learn which side owns the value and recover a value with that side's exact bound, without converting through an unchecked Nat.

Functions

bounded_left_at(left: Nat, right: Nat, value: Bounded(left)) -> BoundedSum(left, right)

 fn bounded_left_at(left: Nat, right: Nat, value: Bounded(left)) -> BoundedSum(left, right)

Wrap a left-side bounded value in BoundedSum with explicit erased bounds.

bounded_right_at(left: Nat, right: Nat, value: Bounded(right)) -> BoundedSum(left, right)

 fn bounded_right_at(left: Nat, right: Nat, value: Bounded(right)) -> BoundedSum(left, right)

Wrap a right-side bounded value in BoundedSum with explicit erased bounds.

fold_sum(left: Nat, right: Nat, a: Type, side: BoundedSum(left, right), on_left: Function(Bounded(left), a), on_right: Function(Bounded(right), a)) -> a

 fn fold_sum(left: Nat, right: Nat, a: Type, side: BoundedSum(left, right), on_left: Function(Bounded(left), a), on_right: Function(Bounded(right), a)) -> a

Eliminate a BoundedSum by applying on_left to a left value or on_right to a right value.

inject_left(n: Nat, m: Nat, value: Bounded(n)) -> Bounded(plus(n, m))

 fn inject_left(n: Nat, m: Nat, value: Bounded(n)) -> Bounded(plus(n, m))

Embed the left side of a disjoint n + m state space.

inject_left_at(left: Nat, right: Nat, value: Bounded(left)) -> Bounded(plus(left, right))

 fn inject_left_at(left: Nat, right: Nat, value: Bounded(left)) -> Bounded(plus(left, right))

Embed a value from the left bound into a disjoint left + right space with explicit erased bounds.

inject_right(n: Nat, m: Nat, value: Bounded(m)) -> Bounded(plus(n, m))

 fn inject_right(n: Nat, m: Nat, value: Bounded(m)) -> Bounded(plus(n, m))

Embed the right side of a disjoint n + m state space. Each constructor in the left bound shifts the right value once, making the two images disjoint by construction.

inject_right_at(left: Nat, right: Nat, value: Bounded(right)) -> Bounded(plus(left, right))

 fn inject_right_at(left: Nat, right: Nat, value: Bounded(right)) -> Bounded(plus(left, right))

Embed a value from the right bound into a disjoint left + right space with an explicit erased right bound.

lift_bounded_sum(left: Nat, right: Nat, side: BoundedSum(left, right)) -> BoundedSum(S(left), right)

 fn lift_bounded_sum(left: Nat, right: Nat, side: BoundedSum(left, right)) -> BoundedSum(S(left), right)

Increment the left bound of a BoundedSum, shifting left values by Next and leaving right values unchanged.

lift_bounded_sum_equivalent(left: Nat, right: Nat, source: BoundedSum(left, right), target: BoundedSum(left, right), equality: Equivalent(BoundedSum(left, right), source, target)) -> Equivalent(BoundedSum(S(left), right), lift_bounded_sum(left, right, source), lift_bounded_sum(left, right, target))

 fn lift_bounded_sum_equivalent(left: Nat, right: Nat, source: BoundedSum(left, right), target: BoundedSum(left, right), equality: Equivalent(BoundedSum(left, right), source, target)) -> Equivalent(BoundedSum(S(left), right), lift_bounded_sum(left, right, source), lift_bounded_sum(left, right, target))

Congruence lemma lifting an equivalence of BoundedSum values through lift_bounded_sum.

split_injected_left(left: Nat, right: Nat, value: Bounded(left)) -> Equivalent(BoundedSum(left, right), split_sum(left, inject_left_at(left, right, value)), bounded_left_at(left, right, value))

 fn split_injected_left(left: Nat, right: Nat, value: Bounded(left)) -> Equivalent(BoundedSum(left, right), split_sum(left, inject_left_at(left, right, value)), bounded_left_at(left, right, value))

The structural splitter is a left inverse of both disjoint injections. These equations are the elimination laws needed by dependent consumers: once a combined state is already known to come from one side, matching the proof reduces split_sum to that exact side and value.

split_injected_left_at(left: Nat, right: Nat, value: Bounded(left)) -> Equivalent(BoundedSum(left, right), split_sum(left, inject_left_at(left, right, value)), bounded_left_at(left, right, value))

 fn split_injected_left_at(left: Nat, right: Nat, value: Bounded(left)) -> Equivalent(BoundedSum(left, right), split_sum(left, inject_left_at(left, right, value)), bounded_left_at(left, right, value))

Proof that split_sum recovers the original value after inject_left_at, taking an explicit erased right bound.

split_injected_right(left: Nat, right: Nat, value: Bounded(right)) -> Equivalent(BoundedSum(left, right), split_sum(left, inject_right_at(left, right, value)), bounded_right_at(left, right, value))

 fn split_injected_right(left: Nat, right: Nat, value: Bounded(right)) -> Equivalent(BoundedSum(left, right), split_sum(left, inject_right_at(left, right, value)), bounded_right_at(left, right, value))

Proof that split_sum recovers the original value after inject_right_at.

split_sum(left: Nat, right: Nat, value: Bounded(plus(left, right))) -> BoundedSum(left, right)

 fn split_sum(left: Nat, right: Nat, value: Bounded(plus(left, right))) -> BoundedSum(left, right)

Split a bounded value in left + right into either BoundedLeft or BoundedRight.

widen(n: Nat, extra: Nat, value: Bounded(n)) -> Bounded(plus(n, extra))

 fn widen(n: Nat, extra: Nat, value: Bounded(n)) -> Bounded(plus(n, extra))

Preserve a bounded value while extending its upper bound on the right. The recursion follows the value itself, so no unchecked conversion from Nat is involved.