Library Coq.Vectors.VectorDef
Definitions of Vectors and functions to use them
Author: Pierre Boutillier
Institution: PPS, INRIA 12/2010
Names should be "caml name in list.ml" if exists and order of arguments
have to be the same. complain if you see mistakes ...
A vector is a list of size n whose elements belong to a set A.
Inductive t A :
nat ->
Type :=
|
nil :
t A 0
|
cons :
forall (
h:
A) (
n:
nat),
t A n ->
t A (
S n).
Local Notation "[]" := (
nil _).
Local Notation "h :: t" := (
cons _ h _ t) (
at level 60,
right associativity).
Section SCHEMES.
An induction scheme for non-empty vectors
An induction scheme for 2 vectors of same length
A vector of length 0 is nil
A vector of length S _ is cons
The first element of a non empty vector
Definition hd {
A} {
n} (
v:
t A (
S n)) :=
Eval cbv delta beta in
(
caseS (
fun n v =>
A) (
fun h n t =>
h)
v).
The last element of an non empty vector
Definition last {
A} {
n} (
v :
t A (
S n)) :=
Eval cbv delta in
(
rectS (
fun _ _ =>
A) (
fun a =>
a) (
fun _ _ _ H =>
H)
v).
Build a vector of n{^ th} a
The
p{^ th} element of a vector of length
m.
Computational behavior of this function should be the same as
ocaml function.
An equivalent definition of nth.
Put a at the p{^ th} place of v
Version of replace with lt
Remove the first element of a non empty vector
Definition tl {
A} {
n} (
v:
t A (
S n)) :=
Eval cbv delta beta in
(
caseS (
fun n v =>
t A n) (
fun h n t =>
t)
v).
Remove last element of a non-empty vector
Add an element at the end of a vector
Copy last element of a vector
Remove p last elements of a vector
Concatenation of two vectors
Two definitions of the tail recursive function that appends two lists but
reverses the first one
This one has the exact expected computational behavior
This one has a better type
rev
a₁ ; a₂ ; .. ; an is
an ; a{n-1} ; .. ; a₁
Caution : There is a lot of rewrite garbage in this definition
Here are special non dependent useful instantiation of induction
schemes
Uniform application on the arguments of the vector
map2 g x1 .. xn y1 .. yn = (g x1 y1) .. (g xn yn)
fold_left f b x1 .. xn = f .. (f (f b x1) x2) .. xn
fold_right f x1 .. xn b = f x1 (f x2 .. (f xn b) .. )
fold_right2 g x1 .. xn y1 .. yn c = g x1 y1 (g x2 y2 .. (g xn yn c) .. )
fold_left2 f b x1 .. xn y1 .. yn = g .. (g (g a x1 y1) x2 y2) .. xn yn
vector <=> list functions