aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Dénès2017-03-30 00:18:10 +0200
committerMaxime Dénès2017-03-30 00:18:10 +0200
commitab9e14c9e61aee9812a1b30795942a0a2bea63de (patch)
treee7b9d8a1163e3bfcb39c137f7c9ecb24fa067d78
parent298b0a90e0393f3af16fd0b5870128212518a1a4 (diff)
parentdfc89e3c2fb5e3801307b5b8b5491a38aa1630bb (diff)
Merge PR#469: Added take to VectorDef
-rw-r--r--theories/Vectors/VectorDef.v10
-rw-r--r--theories/Vectors/VectorSpec.v29
2 files changed, 39 insertions, 0 deletions
diff --git a/theories/Vectors/VectorDef.v b/theories/Vectors/VectorDef.v
index 1f8b76cb62..c494517766 100644
--- a/theories/Vectors/VectorDef.v
+++ b/theories/Vectors/VectorDef.v
@@ -147,6 +147,16 @@ Definition shiftrepeat {A} := @rectS _ (fun n _ => t A (S (S n)))
(fun h => h :: h :: []) (fun h _ _ H => h :: H).
Global Arguments shiftrepeat {A} {n} v.
+(** Take first [p] elements of a vector *)
+Fixpoint take {A} {n} (p:nat) (le:p <= n) (v:t A n) : t A p :=
+ match p as p return p <= n -> t A p with
+ | 0 => fun _ => []
+ | S p' => match v in t _ n return S p' <= n -> t A (S p') with
+ | []=> fun le => False_rect _ (Nat.nle_succ_0 p' le)
+ | x::xs => fun le => x::take p' (le_S_n p' _ le) xs
+ end
+ end le.
+
(** Remove [p] last elements of a vector *)
Lemma trunc : forall {A} {n} (p:nat), n > p -> t A n
-> t A (n - p).
diff --git a/theories/Vectors/VectorSpec.v b/theories/Vectors/VectorSpec.v
index c5278b918f..869d0fb5af 100644
--- a/theories/Vectors/VectorSpec.v
+++ b/theories/Vectors/VectorSpec.v
@@ -122,3 +122,32 @@ induction l.
- reflexivity.
- unfold to_list; simpl. now f_equal.
Qed.
+
+Lemma take_O : forall {A} {n} le (v:t A n), take 0 le v = [].
+Proof.
+ reflexivity.
+Qed.
+
+Lemma take_idem : forall {A} p n (v:t A n) le le',
+ take p le' (take p le v) = take p le v.
+Proof.
+ induction p; intros n v le le'.
+ - auto.
+ - destruct v. inversion le. simpl. apply f_equal. apply IHp.
+Qed.
+
+Lemma take_app : forall {A} {n} (v:t A n) {m} (w:t A m) le, take n le (append v w) = v.
+Proof.
+ induction v; intros m w le.
+ - reflexivity.
+ - simpl. apply f_equal. apply IHv.
+Qed.
+
+(* Proof is irrelevant for [take] *)
+Lemma take_prf_irr : forall {A} p {n} (v:t A n) le le', take p le v = take p le' v.
+Proof.
+ induction p; intros n v le le'.
+ - reflexivity.
+ - destruct v. inversion le. simpl. apply f_equal. apply IHp.
+Qed.
+