Boxed vector type. Lean's Array is already the equivalent of Haskell's Vector.
$$\text{Vector}\ \alpha := \text{Array}\ \alpha$$
Equations
- Data.Vector α = Array α
Instances For
The empty vector. $$\text{empty} : \text{Vector}\ \alpha,\quad |\text{empty}| = 0$$
Equations
Instances For
A vector containing a single element. $$\text{singleton}(x) = [x]$$
Equations
Instances For
Convert a list to a vector. $$\text{fromList}([x_1, \ldots, x_n]) = [x_1, \ldots, x_n]$$
Equations
- Data.Vector.fromList xs = xs.toArray
Instances For
Create a vector of n copies of element x.
$$\text{replicate}(n, x) = [x, x, \ldots, x],\quad |\text{result}| = n$$
Equations
- Data.Vector.replicate n x = (List.replicate n x).toArray
Instances For
Create a vector of length n by applying f to each index.
$$\text{generate}(n, f) = [f(0), f(1), \ldots, f(n-1)]$$
Equations
- Data.Vector.generate n f = Data.Vector.generate.go f n 0 #[]
Instances For
Equations
- Data.Vector.generate.go f 0 a✝¹ a✝ = a✝
- Data.Vector.generate.go f fuel.succ a✝¹ a✝ = Data.Vector.generate.go f fuel (a✝¹ + 1) (Array.push a✝ (f a✝¹))
Instances For
The number of elements. $$\text{length}(v) = |v|$$
Equations
- v.length = Array.size v
Instances For
Is the vector empty? $$\text{null}(v) \iff |v| = 0$$
Equations
- v.null = (Array.size v == 0)
Instances For
The first element of a non-empty vector. $$\text{head}(v) = v_0,\quad \text{requires } |v| > 0$$
Instances For
The last element of a non-empty vector. $$\text{last}(v) = v_{|v|-1},\quad \text{requires } |v| > 0$$
Instances For
All elements except the last. $$\text{init}(v) = v[0..|v|-1],\quad \text{requires } |v| > 0$$
Instances For
All elements except the first. $$\text{tail}(v) = v[1..],\quad \text{requires } |v| > 0$$
Instances For
Apply a function to every element. $$\text{map}(f, [x_1, \ldots, x_n]) = [f(x_1), \ldots, f(x_n)]$$
Equations
- Data.Vector.map f v = Array.map f v
Instances For
Map with index. $$\text{imap}(f, [x_0, \ldots, x_{n-1}]) = [f(0, x_0), \ldots, f(n-1, x_{n-1})]$$
Equations
- Data.Vector.imap f v = Data.Vector.imap.go f v 0 #[]
Instances For
Equations
- Data.Vector.imap.go f v i acc = if h : i < Array.size v then Data.Vector.imap.go f v (i + 1) (acc.push (f i v[i])) else acc
Instances For
Map and concatenate. $$\text{concatMap}(f, v) = \text{concat}(\text{map}(f, v))$$
Equations
- Data.Vector.concatMap f v = Array.foldl (fun (acc : Data.Vector β) (x : α) => acc ++ f x) #[] v
Instances For
Keep elements satisfying a predicate. $$\text{filter}(p, v) = [x \in v \mid p(x)]$$
Equations
- Data.Vector.filter p v = Array.filter p v
Instances For
Filter with index. $$\text{ifilter}(p, v) = [v_i \mid p(i, v_i)]$$
Equations
- Data.Vector.ifilter p v = Data.Vector.ifilter.go p v 0 #[]
Instances For
Strict left fold. $$\text{foldl'}(f, z, [x_1, \ldots, x_n]) = f(\ldots f(f(z, x_1), x_2) \ldots, x_n)$$
Equations
- Data.Vector.foldl' f z v = Array.foldl f z v
Instances For
Strict left fold on non-empty vector using first element as seed. $$\text{foldl1'}(f, [x_1, \ldots, x_n]) = f(\ldots f(x_1, x_2) \ldots, x_n)$$
Equations
- Data.Vector.foldl1' f v h = Data.Vector.foldl1'.go f v 1 v[0]
Instances For
Equations
- Data.Vector.foldl1'.go f v i acc = if hi : i < Array.size v then Data.Vector.foldl1'.go f v (i + 1) (f acc v[i]) else acc
Instances For
Right fold. $$\text{foldr}(f, z, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_n, z)))$$
Equations
- Data.Vector.foldr f z v = Array.foldr f z v
Instances For
Right fold on non-empty vector using last element as seed. $$\text{foldr1}(f, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_{n-1}, x_n)))$$
Equations
- Data.Vector.foldr1 f v h = Data.Vector.foldr1.go f v (Array.size v - 2) v[Array.size v - 1]
Instances For
Equations
- Data.Vector.foldr1.go f v 0 a✝ = if h0 : 0 < Array.size v then f v[0] a✝ else a✝
- Data.Vector.foldr1.go f v i.succ a✝ = if hi : i + 1 < Array.size v then Data.Vector.foldr1.go f v i (f v[i + 1] a✝) else a✝
Instances For
Strict left fold with index. $$\text{ifoldl'}(f, z, v) = f(\ldots f(f(z, 0, v_0), 1, v_1) \ldots, n-1, v_{n-1})$$
Equations
- Data.Vector.ifoldl' f z v = Data.Vector.ifoldl'.go f v 0 z
Instances For
Equations
- Data.Vector.ifoldl'.go f v i acc = if h : i < Array.size v then Data.Vector.ifoldl'.go f v (i + 1) (f acc i v[i]) else acc
Instances For
Right fold with index. $$\text{ifoldr}(f, z, v) = f(0, v_0, f(1, v_1, \ldots f(n-1, v_{n-1}, z)))$$
Equations
- Data.Vector.ifoldr f z v = Data.Vector.ifoldr.go f v (Array.size v) z
Instances For
Equations
- Data.Vector.ifoldr.go f v 0 a✝ = a✝
- Data.Vector.ifoldr.go f v i.succ a✝ = if h : i < Array.size v then Data.Vector.ifoldr.go f v i (f i v[i] a✝) else a✝
Instances For
Do all elements satisfy the predicate? $$\text{all}(p, v) = \forall x \in v.\; p(x)$$
Equations
- Data.Vector.all p v = Array.all v p
Instances For
Does any element satisfy the predicate? $$\text{any}(p, v) = \exists x \in v.\; p(x)$$
Equations
- Data.Vector.any p v = Array.any v p
Instances For
Sum of all elements. $$\text{sum}(v) = \sum v$$
Equations
- v.sum = Array.foldl (fun (x1 x2 : α) => x1 + x2) 0 v
Instances For
Product of all elements. $$\text{product}(v) = \prod v$$
Equations
- v.product = Array.foldl (fun (x1 x2 : α) => x1 * x2) 1 v
Instances For
Maximum element of a non-empty vector. $$\text{maximum}(v) = \max(v),\quad \text{requires } |v| > 0$$
Equations
- v.maximum h = Data.Vector.maximum.go v 1 v[0]
Instances For
Equations
- Data.Vector.maximum.go v i best = if hi : i < Array.size v then have x := v[i]; Data.Vector.maximum.go v (i + 1) (if (compare x best == Ordering.gt) = true then x else best) else best
Instances For
Minimum element of a non-empty vector. $$\text{minimum}(v) = \min(v),\quad \text{requires } |v| > 0$$
Equations
- v.minimum h = Data.Vector.minimum.go v 1 v[0]
Instances For
Equations
- Data.Vector.minimum.go v i best = if hi : i < Array.size v then have x := v[i]; Data.Vector.minimum.go v (i + 1) (if (compare x best == Ordering.lt) = true then x else best) else best
Instances For
Does the element occur in the vector? $$\text{elem}(x, v) = \exists i.\; v_i = x$$
Equations
- Data.Vector.elem x v = Array.any v fun (x_1 : α) => x_1 == x
Instances For
Does the element NOT occur in the vector? $$\text{notElem}(x, v) = \neg\text{elem}(x, v)$$
Equations
- Data.Vector.notElem x v = !Data.Vector.elem x v
Instances For
Find the first element satisfying a predicate. $$\text{find}(p, v)$$
Equations
- Data.Vector.find p v = Data.Vector.find.go p v 0
Instances For
Find the index of the first element satisfying a predicate. $$\text{findIndex}(p, v)$$
Equations
- Data.Vector.findIndex p v = Data.Vector.findIndex.go p v 0
Instances For
Zip two vectors with a combining function. $$\text{zipWith}(f, u, v) = [f(u_0, v_0), f(u_1, v_1), \ldots]$$
Equations
- Data.Vector.zipWith f u v = (List.zipWith f u.toList v.toList).toArray
Instances For
Unzip a vector of pairs into a pair of vectors. $$\text{unzip}([(a_1, b_1), \ldots]) = ([a_1, \ldots], [b_1, \ldots])$$
Instances For
Reverse the elements. $$\text{reverse}([x_1, \ldots, x_n]) = [x_n, \ldots, x_1]$$
Equations
- v.reverse = Array.reverse v
Instances For
Permute elements according to an index vector. $$\text{backpermute}(v, is) = [v_{is_0}, v_{is_1}, \ldots]$$ Out-of-bounds indices produce a default value.
Equations
- v.backpermute is = Array.map (fun (i : Nat) => if h : i < Array.size v then v[i] else default) is
Instances For
Modify the element at index i by applying f.
$$\text{modify}(f, i, v)$$ returns $v$ with $v_i$ replaced by $f(v_i)$.
If i is out of bounds, returns v unchanged.
Equations
- Data.Vector.modify f i v = if h : i < Array.size v then Array.set v i (f v[i]) h else v
Instances For
Append an element to the end. $$\text{snoc}(v, x) = [v_0, \ldots, v_{n-1}, x]$$
Equations
- v.snoc x = Array.push v x
Instances For
Prepend an element to the front. $$\text{cons}(x, v) = [x, v_0, \ldots, v_{n-1}]$$
Equations
- Data.Vector.cons x v = #[x] ++ v