-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.v
90 lines (77 loc) · 2.14 KB
/
vec.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Definition admit {T: Set}: T. Admitted.
Inductive nat: Set :=
| O: nat
| S: nat -> nat.
Inductive vec (A: Set): nat -> Set :=
| nil : vec A (O )
| cons: forall m, A -> vec A m -> vec A (S m).
Arguments nil {A}.
Arguments cons {A m} _ _.
Infix "::" := cons.
Inductive eq {A: Set} (x: A): A -> Set :=
| eq_refl: eq x x.
Arguments eq_refl {A x}.
Infix "=" := eq : type_scope.
Definition succ_rewrite {T: nat -> Set} {m n: nat} (H: S m = S n): T n -> T m.
refine (
match H in _ = j return
match j with
| O => Empty_set
| S i => T i -> T m
end
with
| eq_refl => fun t => t
end
).
Defined.
Definition nat_disc {T: Set} {m: nat} (H: S m = O): T.
refine (
match H in _ = j return
match j with
| O => T
| S i => unit
end
with
| eq_refl => tt
end
).
Defined.
Definition zip' {A B C: Set} (f: A -> B -> C): forall n, vec A n -> vec B n -> vec C n.
refine (
fix zip {n} xs :=
match xs in vec _ j return vec B j -> vec C j with
| nil => fun ys => nil
| cons p x xt => fun ys =>
match ys in vec _ j return S p = j -> vec C (S p) with
| nil => nat_disc
| cons q y yt => fun Heq => f x y :: zip xt (succ_rewrite Heq yt)
end eq_refl
end
).
Defined.
Definition uncons {A: Set} {m: nat} (v: vec A (S m)): A * vec A m.
refine (
match v in vec _ j return
match j with
| O => unit
| S i => A * vec A i
end
with
| nil => tt
| cons p x xt => (x, xt)
end
).
Defined.
Definition zip {A B C: Set} (f: A -> B -> C): forall n, vec A n -> vec B n -> vec C n.
refine (
fix zip {n}: vec A n -> vec B n -> vec C n :=
match n with
| O => fun xs ys => nil
| S p => fun xs ys =>
match uncons xs, uncons ys with
| (x, xt), (y, yt) => f x y :: zip xt yt
end
end
).
Defined.
Print zip.