65 lines
1.3 KiB
Coq
65 lines
1.3 KiB
Coq
Require Import List.
|
|
Import ListNotations.
|
|
|
|
Definition lstHd {A: Type} (ls: list A)
|
|
: length ls <> 0 -> A.
|
|
Proof.
|
|
refine (
|
|
match ls with
|
|
| [] => fun pf => _
|
|
| x :: _ => fun _ => x
|
|
end).
|
|
contradiction.
|
|
Defined.
|
|
|
|
Module Vec.
|
|
Definition t (A: Type) (sz: nat): Type :=
|
|
{ls: list A | length ls = sz}.
|
|
|
|
Context {A: Type} {sz: nat}.
|
|
|
|
Definition hd (v: t A (S sz)): A.
|
|
refine (
|
|
match v with
|
|
| exist _ l pf => _
|
|
end).
|
|
induction l.
|
|
- discriminate pf.
|
|
- exact a.
|
|
Show Proof.
|
|
Defined.
|
|
|
|
Definition tl (v: t A (S sz)): t A sz.
|
|
refine (let (l, _) := v in _).
|
|
induction l.
|
|
- discriminate e.
|
|
- simpl in e.
|
|
Search (S _ = S _).
|
|
rewrite (PeanoNat.Nat.succ_inj (length l) sz) in e.
|
|
+
|
|
f_equal in e.
|
|
exact (exist _ l .
|
|
Defined.
|
|
|
|
Fixpoint zip {A B: Type} {n:nat}
|
|
(av: Vec A n) (bv: Vec B n): Vec (A*B) n :=
|
|
match av with
|
|
| [] => []
|
|
| a::av' =>
|
|
|
|
Definition dotProduct {n:nat}
|
|
(v1 v2: Vec bool n): csr.A :=
|
|
let v12 := zip v1 v2 in
|
|
let ires := Vector.map (fun '(a, b) => csr.multn a b) v12 in
|
|
fold_right (fun elem res => csr.addtn elem res) ires csr.zero.
|
|
End Vec.
|
|
Definition
|
|
|
|
|
|
|
|
match av in Vec _ n
|
|
return Vec B n -> Vec (A*B) n with
|
|
| [] => fun _ => []
|
|
| a::av' => fun bv => (a, Vector.hd bv) :: zip av' (Vector.tl bv)
|
|
end bv.
|