module Data.Vec.Base where
open import Data.List.Length public -- we need reexport make-irr for []v to work open Data.Irr using (make-irr) public open Data.Irr private variable β : Level A B C : Type β n k : Nat
Vectorsπ
The type Vec is a
representation of n-ary tuples with coordinates drawn from A.
record Vec {β} (A : Type β) (n : Nat) : Type β where constructor vec field lower : List A β¦ len β¦ : Irr (Length lower n) pattern []v = vec [] infixr 20 _β·v_ _β·v_ : β {n} β A β Vec A n β Vec A (suc n) _β·v_ v (vec vs β¦ p β¦) = vec (v β· vs) β¦ suc <$> p β¦ data Vec-view {β} {A : Type β} : {n : Nat} β Vec A n β TypeΟ where [] : Vec-view []v _β·_ : β {n} a β (vs : Vec A n) β Vec-view {n = suc n} (a β·v vs) vec-view : β {n} (v : Vec A n) β Vec-view v vec-view {n = zero} []v = [] vec-view {n = zero} (vec (x β· xs) β¦ l β¦) with () β recover l vec-view {n = suc n} (vec (x β· xs)) = x β· vec xs β¦ len-uncons <$> auto β¦ vec-view {n = suc n} (vec [] β¦ l β¦) with () β recover l listβvec : (xs : List A) β Vec A (length xs) listβvec xs = vec xs head : Vec A (suc n) β A head (vec (x β· xs)) = x head (vec [] β¦ l β¦) with () β recover l tail : Vec A (suc n) β Vec A n tail v with (x β· xs) β vec-view v = xs
The type Vec A n is
equivalent to the type
i.e., the functions from the standard finite set with
elements to the type
The halves of this equivalence are called lookup and tabulate.
lookup : Vec A n β Fin n β A lookup (vec xs) (fin n) = from-just! _ $ List.!?-just xs n p where abstract p : n Nat.< length xs p = β€-trans auto $ subst (Nat._β€ length xs) (has-length auto) auto
List syntaxπ
A similar type to Vec can be
defined by recursion as an iteraded non-dependent product. The resulting type
Vecβ has the advantage of
supporting usual tuple syntax, but is fiddlier to eliminate. This is
solved by implementing the From-product typeclass for
Vec, which enables list syntax
for the latter.
instance From-prod-Vec : From-product A (Vec A) From-prod-Vec .From-product.from-prod = go where go : β n β Vecβ A n β Vec A n go zero xs = []v go (suc zero) xs = xs β·v []v go (suc (suc n)) (x , xs) = x β·v go (suc n) xs _++_ : β {n k} β Vec A n β Vec A k β Vec A (n + k) vec xs ++ vec ys = vec (xs List.++ ys) β¦ liftA2 len-++ auto auto β¦ Vec-elim : β {β β'} {A : Type β} (P : β {n} β Vec A n β Type β') β P []v β (β {n} x (xs : Vec A n) β P xs β P (x β·v xs)) β β {n} (xs : Vec A n) β P xs Vec-elim P p[] pβ· v with vec-view v ... | [] = p[] ... | (x β· xs) = pβ· x xs $ Vec-elim P p[] pβ· xs
Vec-cast : {x y : Nat} β x β‘ y β Vec A x β Vec A y Vec-cast {A = A} {x = x} {y = y} p (vec l β¦ len β¦) = vec l β¦ subst (Ξ» n β Length l n) p <$> len β¦
tabulate : (Fin n β A) β Vec A n tabulate v = vec (List.tabulate v) β¦ forget (len-tabulate v) β¦ instance Map-Vec : β {n} β Map (eff (Ξ» A β Vec A n ) ) Map-Vec .Map.map f (vec l) = vec (f <$> l) β¦ len-map <$> auto β¦ zip : Vec A n β Vec B n β Vec (A Γ B) n zip (vec u) (vec v) = vec (List.zip u v) β¦ liftA2 len-zip auto auto β¦ zip-with : (A β B β C) β Vec A n β Vec B n β Vec C n zip-with f (vec u) (vec v) = vec (List.zip-with f u v) β¦ liftA2 len-zip-with auto auto β¦ replicate : (n : Nat) β A β Vec A n replicate zero a = []v replicate (suc n) a = a β·v replicate n a _ : Path (Vec Nat 3) [ 1 , 2 , 3 ] (1 β·v 2 β·v 3 β·v []v) _ = refl