1
1
From Bits Require Import operations.
2
2
From Bits Require Import spec.
3
3
4
+ From Coq Require Import FunctionalExtensionality.
4
5
From Coq Require Import Lists.List.
5
6
From Coq Require Numbers.NatInt.NZLog.
7
+ From Coq Require Import Peano_dec.
6
8
From Coq Require Import PeanoNat.
7
9
From Coq Require Import Strings.String.
8
10
From Coq Require Import Vectors.Vector.
@@ -36,6 +38,27 @@ Constraint Vec.u1 <= mkrel.u0.
36
38
Constraint Vec.u1 <= mkapp.u0.
37
39
Constraint Vec.u1 <= mkapp.u1.
38
40
41
+ Lemma Vec_0_nil :
42
+ forall (a : Type) (v : Vec 0 a),
43
+ v = [].
44
+ Proof .
45
+ intros a v.
46
+ apply (case0 (fun v' => v' = [])).
47
+ reflexivity.
48
+ Qed .
49
+
50
+ Lemma Vec_S_cons :
51
+ forall (n : nat) (a : Type) (v : Vec (S n) a),
52
+ exists (x : a) (xs : Vec n a),
53
+ v = x::xs.
54
+ Proof .
55
+ intros n a v.
56
+ apply (caseS (fun n' v' => exists (x : a) (xs : Vec n' a), v' = x::xs)).
57
+ intros x m xs.
58
+ exists x. exists xs.
59
+ reflexivity.
60
+ Qed .
61
+
39
62
Fixpoint gen (n : nat) (a : Type) (f : nat -> a) {struct n} : Vec n a.
40
63
refine (
41
64
match n with
@@ -115,6 +138,15 @@ Fixpoint atWithDefault (n : nat) (a : Type) (default : a) (v : Vec n a) (index :
115
138
).
116
139
Defined .
117
140
141
+ Fixpoint atWithProof (n : nat) (a : Type ) (v : Vec n a) (i : nat) :
142
+ IsLtNat i n -> a :=
143
+ match i as i', n as n' return Vec n' a -> IsLtNat i' n' -> a with
144
+ | _, O => fun _ prf =>
145
+ match Nat.nlt_0_r _ prf with end
146
+ | O, S y => fun v' prf => Vector.hd v'
147
+ | S x, S y => fun v' prf => atWithProof y a (Vector.tl v') x (le_S_n _ _ prf)
148
+ end v.
149
+
118
150
Definition map (a b : Type ) (f : a -> b) (n : nat) (xs : Vec n a) :=
119
151
VectorDef.map f xs.
120
152
@@ -175,6 +207,15 @@ Proof.
175
207
}
176
208
Qed .
177
209
210
+ Fixpoint genWithProof (n : nat) (a : Type ) :
211
+ (forall (i : nat), IsLtNat i n -> a) -> Vec n a :=
212
+ match n as n' return (forall (i : nat), IsLtNat i n' -> a) -> Vec n' a with
213
+ | O => fun _ => Vector.nil a
214
+ | S m => fun f => Vector.cons a (f 0 (le_n_S _ _ (le_0_n _)))
215
+ m (genWithProof m a
216
+ (fun i prf => f (S i) (le_n_S _ _ prf)))
217
+ end .
218
+
178
219
(* NOTE: This version of `zip` accepts two vectors of different size, unlike the
179
220
* one in `CoqVectorsExtra.v` *)
180
221
Fixpoint zipFunctional (a b : sort 0) (m n : nat) (xs : Vec m a) (ys : Vec n b)
@@ -199,6 +240,41 @@ Definition zipWithFunctional
199
240
(a b c : Type ) (f : a -> b -> c) (n : nat) (xs : Vec n a) (ys : Vec n b) :=
200
241
VectorDef.map (fun p => f (fst p) (snd p)) (zipFunctional _ _ _ _ xs ys).
201
242
243
+ Lemma at_gen_Vec :
244
+ forall (n : nat) (a : Type )
245
+ (f : forall i : nat, IsLtNat i n -> a)
246
+ (ix : nat) (pf : IsLtNat ix n),
247
+ atWithProof n a (genWithProof n a f) ix pf = f ix pf.
248
+ Proof .
249
+ intros n a f.
250
+ induction n; intros ix pf.
251
+ - destruct (Nat.nlt_0_r ix pf).
252
+ - induction ix.
253
+ + simpl.
254
+ rewrite (le_unique _ _ (le_n_S 0 n (le_0_n n)) pf).
255
+ reflexivity.
256
+ + simpl.
257
+ rewrite IHn.
258
+ rewrite (le_unique _ _ (le_n_S (Succ ix) n (le_S_n (S ix) n pf)) pf).
259
+ reflexivity.
260
+ Qed .
261
+
262
+ Lemma gen_at_Vec :
263
+ forall (n : nat) (a : Type) (x : Vec n a),
264
+ genWithProof n a (atWithProof n a x) = x.
265
+ Proof .
266
+ intros n a x.
267
+ induction n.
268
+ - rewrite (Vec_0_nil a x). reflexivity.
269
+ - destruct (Vec_S_cons n a x) as [y [ys Yeq]].
270
+ subst x. simpl.
271
+ rewrite <- (IHn ys) at 1.
272
+ do 2 f_equal.
273
+ extensionality i. extensionality prf.
274
+ rewrite (le_unique _ _ (le_S_n (S i) n (le_n_S (Succ i) n prf)) prf).
275
+ reflexivity.
276
+ Qed .
277
+
202
278
Notation bitvector n := (Vec n bool).
203
279
204
280
(* NOTE BITS are stored in reverse order than bitvector *)
0 commit comments