Std.Vector (Cure Standard Library v0.37.2)

View Source

Length-indexed vectors.

Vector(a, n) is a real indexed family checked by the dependent kernel. The length is the Nat index, not a runtime tuple field. Erasure leaves the compact constructor spine :empty / {:prepend, head, tail} while the type checker keeps every length relationship precise.

Examples

use Std.Vector

let xs = prepend(1, prepend(2, empty()))
head(xs)                                 # => 1
sum(xs)                                  # => 3
to_list(reverse(xs))                     # => [2, 1]

Summary

Functions

Return true if every element of xs satisfies pred.

Return true if at least one element of xs satisfies pred.

Concatenate two vectors; the resulting length index is plus(m, n).

Compute the runtime Nat length of xs.

Drop the first n elements of a vector of length plus(n, m), keeping the trailing m. Same convoy: with n refines xs, so the Z branch returns xs directly (plus(Z, m) reduces to m, matching the Vector(a, m) result) and the S(k) branch peels one element off the now-non-empty xs.

First element satisfying pred, or None if there is none.

Recover a length-indexed vector from a list. The length is not known statically, so it is packed existentially in a Sigma alongside the vector. The pair is built with the %[..] surface (which lowers to mk_pair while solving the dependent second-component motive from the expected Sigma type); a bare mk_pair(..) leaves that motive an unsolved metavariable.

First element of a non-empty vector. Total: the S(n) index rules out empty.

All-but-the-last element of a non-empty vector, shrinking the length by one.

Last element of a non-empty vector. The S(n) index makes emptiness unrepresentable, so this is total with no Option.

Alias for count/1: return the runtime Nat length of xs.

Total indexed read at a statically in-bounds Bounded(n) position.

Apply f to every element, preserving the vector's length.

Product of an Int vector of any length.

Build a vector of length n with every slot set to x.

Reverse the vector, preserving its length. Each prepend is turned into a snoc on the reversed tail, so the length is provably unchanged (Vector(a, n) -> Vector(a, n)) without any auxiliary plus-lemma.

Replace the element at index with value, preserving the vector's length.

Build a one-element vector containing x.

Append one element at the END of the vector, growing the length by one. Total: the length index tracks the growth exactly, mirroring prepend at the other end. snoc is the workhorse behind reverse.

Sum of an Int vector of any length.

All elements of a non-empty vector after the first, shrinking the length by one.

First n elements of a vector whose length is plus(n, m), dropping the trailing m. with n (not match n) is the Idris-style convoy: matching n refines the sibling xs : Vector(a, plus(n, m)) per branch, so in the S(k) branch xs : Vector(a, plus(S(k), m)) — a non-empty vector — and the inner match xs legitimately omits empty. That omission is discharged because coverage normalizes the scrutinee index: plus(S(k), m) reduces to S(plus(k, m)), whose S head refutes empty's Z.

Forget the length index, projecting to an ordinary List.

Apply f to the element at index, preserving the vector's length.

Pair two equal-length vectors element-wise into a vector of 2-tuples.

Types

type Vector(a) indices (n: Nat)

 type Vector(a) indices (n: Nat)
   empty : Vector(a, Z)
   prepend : a -> Vector(a, n) -> Vector(a, S(n))

Functions

all(a: Type, n: Nat, xs: Vector(a, n), pred: Function(a, Bool)) -> Bool

 fn all(a: Type, n: Nat, xs: Vector(a, n), pred: Function(a, Bool)) -> Bool

Return true if every element of xs satisfies pred.

any(a: Type, n: Nat, xs: Vector(a, n), pred: Function(a, Bool)) -> Bool

 fn any(a: Type, n: Nat, xs: Vector(a, n), pred: Function(a, Bool)) -> Bool

Return true if at least one element of xs satisfies pred.

append(a: Type, m: Nat, n: Nat, xs: Vector(a, m), ys: Vector(a, n)) -> Vector(a, plus(m, n))

 fn append(a: Type, m: Nat, n: Nat, xs: Vector(a, m), ys: Vector(a, n)) -> Vector(a, plus(m, n))

Concatenate two vectors; the resulting length index is plus(m, n).

count(a: Type, n: Nat, xs: Vector(a, n)) -> Nat

 fn count(a: Type, n: Nat, xs: Vector(a, n)) -> Nat

Compute the runtime Nat length of xs.

drop(a: Type, n: Nat, m: Nat, xs: Vector(a, plus(n, m))) -> Vector(a, m)

 fn drop(a: Type, n: Nat, m: Nat, xs: Vector(a, plus(n, m))) -> Vector(a, m)

Drop the first n elements of a vector of length plus(n, m), keeping the trailing m. Same convoy: with n refines xs, so the Z branch returns xs directly (plus(Z, m) reduces to m, matching the Vector(a, m) result) and the S(k) branch peels one element off the now-non-empty xs.

find(a: Type, n: Nat, xs: Vector(a, n), pred: Function(a, Bool)) -> Option(a)

 fn find(a: Type, n: Nat, xs: Vector(a, n), pred: Function(a, Bool)) -> Option(a)

First element satisfying pred, or None if there is none.

foldl(a: Type, b: Type, n: Nat, xs: Vector(a, n), acc: b, f: Function(a, Function(b, b))) -> b

 fn foldl(a: Type, b: Type, n: Nat, xs: Vector(a, n), acc: b, f: Function(a, Function(b, b))) -> b

Left-associative fold over xs starting from acc.

foldr(a: Type, b: Type, n: Nat, xs: Vector(a, n), acc: b, f: Function(a, Function(b, b))) -> b

 fn foldr(a: Type, b: Type, n: Nat, xs: Vector(a, n), acc: b, f: Function(a, Function(b, b))) -> b

Right-associative fold over xs starting from acc.

from_list(a: Type, xs: List(a)) -> Sigma(n: Nat, Vector(a, n))

 fn from_list(a: Type, xs: List(a)) -> Sigma(n: Nat, Vector(a, n))

Recover a length-indexed vector from a list. The length is not known statically, so it is packed existentially in a Sigma alongside the vector. The pair is built with the %[..] surface (which lowers to mk_pair while solving the dependent second-component motive from the expected Sigma type); a bare mk_pair(..) leaves that motive an unsolved metavariable.

head(a: Type, n: Nat, xs: Vector(a, S(n))) -> a

 fn head(a: Type, n: Nat, xs: Vector(a, S(n))) -> a

First element of a non-empty vector. Total: the S(n) index rules out empty.

init(a: Type, n: Nat, xs: Vector(a, S(n))) -> Vector(a, n)

 fn init(a: Type, n: Nat, xs: Vector(a, S(n))) -> Vector(a, n)

All-but-the-last element of a non-empty vector, shrinking the length by one.

is_empty(a: Type, n: Nat, xs: Vector(a, n)) -> Bool

 fn is_empty(a: Type, n: Nat, xs: Vector(a, n)) -> Bool

Return true when xs is empty.

last(a: Type, n: Nat, xs: Vector(a, S(n))) -> a

 fn last(a: Type, n: Nat, xs: Vector(a, S(n))) -> a

Last element of a non-empty vector. The S(n) index makes emptiness unrepresentable, so this is total with no Option.

length(a: Type, n: Nat, xs: Vector(a, n)) -> Nat

 fn length(a: Type, n: Nat, xs: Vector(a, n)) -> Nat

Alias for count/1: return the runtime Nat length of xs.

lookup(a: Type, n: Nat, xs: Vector(a, n), index: Bounded(n)) -> a

 fn lookup(a: Type, n: Nat, xs: Vector(a, n), index: Bounded(n)) -> a

Total indexed read at a statically in-bounds Bounded(n) position.

map(a: Type, b: Type, n: Nat, xs: Vector(a, n), f: Function(a, b)) -> Vector(b, n)

 fn map(a: Type, b: Type, n: Nat, xs: Vector(a, n), f: Function(a, b)) -> Vector(b, n)

Apply f to every element, preserving the vector's length.

product(n: Nat, xs: Vector(Int, n)) -> Int

 fn product(n: Nat, xs: Vector(Int, n)) -> Int

Product of an Int vector of any length.

replicate(a: Type, n: Nat, x: a) -> Vector(a, n)

 fn replicate(a: Type, n: Nat, x: a) -> Vector(a, n)

Build a vector of length n with every slot set to x.

reverse(a: Type, n: Nat, xs: Vector(a, n)) -> Vector(a, n)

 fn reverse(a: Type, n: Nat, xs: Vector(a, n)) -> Vector(a, n)

Reverse the vector, preserving its length. Each prepend is turned into a snoc on the reversed tail, so the length is provably unchanged (Vector(a, n) -> Vector(a, n)) without any auxiliary plus-lemma.

set(a: Type, n: Nat, xs: Vector(a, n), index: Bounded(n), value: a) -> Vector(a, n)

 fn set(a: Type, n: Nat, xs: Vector(a, n), index: Bounded(n), value: a) -> Vector(a, n)

Replace the element at index with value, preserving the vector's length.

singleton(a: Type, x: a) -> Vector(a, S(Z))

 fn singleton(a: Type, x: a) -> Vector(a, S(Z))

Build a one-element vector containing x.

snoc(a: Type, n: Nat, xs: Vector(a, n), y: a) -> Vector(a, S(n))

 fn snoc(a: Type, n: Nat, xs: Vector(a, n), y: a) -> Vector(a, S(n))

Append one element at the END of the vector, growing the length by one. Total: the length index tracks the growth exactly, mirroring prepend at the other end. snoc is the workhorse behind reverse.

sum(n: Nat, xs: Vector(Int, n)) -> Int

 fn sum(n: Nat, xs: Vector(Int, n)) -> Int

Sum of an Int vector of any length.

tail(a: Type, n: Nat, xs: Vector(a, S(n))) -> Vector(a, n)

 fn tail(a: Type, n: Nat, xs: Vector(a, S(n))) -> Vector(a, n)

All elements of a non-empty vector after the first, shrinking the length by one.

take(a: Type, n: Nat, m: Nat, xs: Vector(a, plus(n, m))) -> Vector(a, n)

 fn take(a: Type, n: Nat, m: Nat, xs: Vector(a, plus(n, m))) -> Vector(a, n)

First n elements of a vector whose length is plus(n, m), dropping the trailing m. with n (not match n) is the Idris-style convoy: matching n refines the sibling xs : Vector(a, plus(n, m)) per branch, so in the S(k) branch xs : Vector(a, plus(S(k), m)) — a non-empty vector — and the inner match xs legitimately omits empty. That omission is discharged because coverage normalizes the scrutinee index: plus(S(k), m) reduces to S(plus(k, m)), whose S head refutes empty's Z.

to_list(a: Type, n: Nat, xs: Vector(a, n)) -> List(a)

 fn to_list(a: Type, n: Nat, xs: Vector(a, n)) -> List(a)

Forget the length index, projecting to an ordinary List.

update(a: Type, n: Nat, xs: Vector(a, n), index: Bounded(n), f: Function(a, a)) -> Vector(a, n)

 fn update(a: Type, n: Nat, xs: Vector(a, n), index: Bounded(n), f: Function(a, a)) -> Vector(a, n)

Apply f to the element at index, preserving the vector's length.

zip(a: Type, b: Type, n: Nat, xs: Vector(a, n), ys: Vector(b, n)) -> Vector(Tuple(a, b), n)

 fn zip(a: Type, b: Type, n: Nat, xs: Vector(a, n), ys: Vector(b, n)) -> Vector(Tuple(a, b), n)

Pair two equal-length vectors element-wise into a vector of 2-tuples.

zip_with(a: Type, b: Type, c: Type, n: Nat, xs: Vector(a, n), ys: Vector(b, n), f: Function(a, Function(b, c))) -> Vector(c, n)

 fn zip_with(a: Type, b: Type, c: Type, n: Nat, xs: Vector(a, n), ys: Vector(b, n), f: Function(a, Function(b, c))) -> Vector(c, n)

Combine two equal-length vectors element-wise using f.