Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent types: Vector of vectors

I'm new to dependent types (I'm trying both Idris and Coq, despite their big differences).

I'm trying to express the following type: given a type T and a sequence of k nats n1, n2, ... nk, a type consisting of k sequences of T with length n1, n2, ... nk respectively.

That is, a vector of k vectors, whose lengths are given by a parameter. Is this possible?

like image 470
lodo Avatar asked Aug 17 '17 16:08

lodo


People also ask

What are the different types of vectors in math?

The various types of vectors are zero vector, unit vector, co-initial vector, position vector, like and unlike vector, collinear vector, equal vector, coplanar vector, displacement vector, negative vector and so on. Also, learn about Vector Algebra here.

How to find if a vector is linearly dependent?

means vectors a, b, c are linearly dependent. Answer: vectors a, b, c are linearly dependent. Example 3. Check whether the vectors a = {1; 1; 1}, b = {1; 2; 0}, c = {0; -1; 2} are linearly independent. Solution: Calculate the coefficients in which a linear combination of these vectors is equal to the zero vector.

What is the determinant of the linearly dependent matrix?

If a set has a zero vector, then it means that the vector set is linearly dependent. If the subset of the vector is linearly dependent, then we can say that the vector itself is linearly dependent The determinant of the linearly dependent matrix is zero. Mathematically we can write it as:

How are vectors represented in physics?

The vectors are represented by the direct pointed line in which the length represents the vector and magnitude and the orientation represents the direction of the vector.


2 Answers

You can do this with a heterogeneous list, as follows.

Require Vector.
Require Import List.
Import ListNotations.

Inductive hlist {A : Type} (B : A -> Type) : list A -> Type :=
| hnil : hlist B []
| hcons : forall a l, B a -> hlist B l -> hlist B (a :: l).

Definition vector_of_vectors (T : Type) (l : list nat) : Type :=
  hlist (Vector.t T) l.

Then if l is your list of lengths, the type vector_of_vectors T l with will the type you describe.

For example, we can construct an element of vector_of_vectors bool [2; 0; 1]:

Section example.
  Definition ls : list nat := [2; 0; 1].

  Definition v : vector_of_vectors bool ls :=
    hcons [false; true]
          (hcons []
                 (hcons [true] hnil)).
End example.

This example uses some notations for vectors that you can set up like this:

Arguments hnil {_ _}.
Arguments hcons {_ _ _ _} _ _.

Arguments Vector.nil {_}.
Arguments Vector.cons {_} _ {_} _.

Delimit Scope vector with vector.
Bind Scope vector with Vector.t.
Notation "[ ]" := (@Vector.nil _) : vector.
Notation "a :: v" := (@Vector.cons _ a _ v) : vector.
Notation " [ x ] " := (Vector.cons x Vector.nil) : vector.
Notation " [ x ; y ; .. ; z ] " :=  (Vector.cons x (Vector.cons y .. (Vector.cons z Vector.nil) ..)) : vector.

Open Scope vector.
like image 199
James Wilcox Avatar answered Sep 22 '22 17:09

James Wilcox


In Idris, besides creating a custom inductive type, we can reuse the standard type of heterogeneous vectors -- HVect:

import Data.HVect

VecVec : Vect k Nat -> Type -> Type
VecVec shape t = HVect $ map (flip Vect t) shape

val : VecVec [3, 2, 1] Bool
val = [[False, False, False], [False, False], [False]] -- the value is found automatically by Idris' proof-search facilities
like image 36
Anton Trunov Avatar answered Sep 23 '22 17:09

Anton Trunov