Debian Codegraph @

src/Util/HList.v

raw
1 From Coq Require Import Morphisms.
2 From Coq Require Import Relation_Definitions.
3 From Coq Require Import List.
4 Require Import Crypto.Util.Decidable.
5 Require Import Crypto.Util.ListUtil.
6 Require Import Crypto.Util.IffT.
7 Require Import Crypto.Util.Tuple.
8 Require Export Crypto.Util.FixCoqMistakes.
9
10 Fixpoint hlist' T n (f : T -> Type) : tuple' T n -> Type :=
11 match n return tuple' _ n -> Type with
12 | 0 => fun T => f T
13 | S n' => fun Ts => (hlist' T n' f (fst Ts) * f (snd Ts))%type
14 end.
15 Global Arguments hlist' {T n} f _.
16
17 Definition hlist {T n} (f : T -> Type) : forall (Ts : tuple T n), Type :=
18 match n return tuple _ n -> Type with
19 | 0 => fun _ => unit
20 | S n' => @hlist' T n' f
21 end.
22
23 Fixpoint hlistP' T n (f : T -> Prop) : tuple' T n -> Prop :=
24 match n return tuple' _ n -> Prop with
25 | 0 => fun T => f T
26 | S n' => fun Ts => (hlistP' T n' f (fst Ts) /\ f (snd Ts))%type
27 end.
28 Global Arguments hlistP' {T n} f _.
29
30 Definition hlistP {T n} (f : T -> Prop) : forall (Ts : tuple T n), Prop :=
31 match n return tuple _ n -> Prop with
32 | 0 => fun _ => True
33 | S n' => @hlistP' T n' f
34 end.
35
36 Fixpoint rhlist' T n (f : T -> Type) : rtuple' T n -> Type :=
37 match n return rtuple' _ n -> Type with
38 | 0 => fun T => f T
39 | S n' => fun Ts => (f (fst Ts) * rhlist' T n' f (snd Ts))%type
40 end.
41 Global Arguments rhlist' {T n} f _.
42
43 Definition rhlist {T n} (f : T -> Type) : forall (Ts : rtuple T n), Type :=
44 match n return rtuple _ n -> Type with
45 | 0 => fun _ => unit
46 | S n' => @rhlist' T n' f
47 end.
48
49 Fixpoint const' {T n F xs} (v : forall x, F x) : @hlist' T n F xs
50 := match n return forall xs, @hlist' T n F xs with
51 | 0 => fun _ => v _
52 | S n' => fun _ => (@const' T n' F _ v, v _)
53 end xs.
54 Definition const {T n F xs} (v : forall x, F x) : @hlist T n F xs
55 := match n return forall xs, @hlist T n F xs with
56 | 0 => fun _ => tt
57 | S n' => fun xs => @const' T n' F xs v
58 end xs.
59
60 (* tuple map *)
61 Fixpoint mapt' {n A F B} (f : forall x : A, F x -> B) : forall {ts : tuple' A n}, hlist' F ts -> tuple' B n
62 := match n return forall ts : tuple' A n, hlist' F ts -> tuple' B n with
63 | 0 => fun ts v => f _ v
64 | S n' => fun ts v => (@mapt' n' A F B f _ (fst v), f _ (snd v))
65 end.
66 Definition mapt {n A F B} (f : forall x : A, F x -> B)
67 : forall {ts : tuple A n}, hlist F ts -> tuple B n
68 := match n return forall ts : tuple A n, hlist F ts -> tuple B n with
69 | 0 => fun ts v => tt
70 | S n' => @mapt' n' A F B f
71 end.
72
73 Lemma map'_mapt' {n A F B C} (g : B -> C) (f : forall x : A, F x -> B)
74 {ts : tuple' A n} (ls : hlist' F ts)
75 : Tuple.map (n:=S n) g (mapt' f ls) = mapt' (fun x v => g (f x v)) ls.
76 Proof.
77 induction n as [|n IHn]; [ reflexivity | ].
78 { simpl @mapt' in *.
79 rewrite <- IHn.
80 rewrite Tuple.map_S; reflexivity. }
81 Qed.
82
83 Lemma map_mapt {n A F B C} (g : B -> C) (f : forall x : A, F x -> B)
84 {ts : tuple A n} (ls : hlist F ts)
85 : Tuple.map g (mapt f ls) = mapt (fun x v => g (f x v)) ls.
86 Proof.
87 destruct n as [|n]; [ reflexivity | ].
88 apply map'_mapt'.
89 Qed.
90
91 Lemma map_is_mapt {n A F B} (f : A -> B) {ts : tuple A n} (ls : hlist F ts)
92 : Tuple.map f ts = mapt (fun x _ => f x) ls.
93 Proof.
94 destruct n as [|n]; simpl; [ destruct ts; reflexivity | ].
95 induction n as [|n IHn]; [ reflexivity | ].
96 { simpl in *; rewrite <- IHn; clear IHn.
97 reflexivity. }
98 Qed.
99
100 Lemma map_is_mapt' {n A F B} (f : A -> B) {ts : tuple A (S n)} (ls : hlist' F ts)
101 : Tuple.map f ts = mapt' (fun x _ => f x) ls.
102 Proof. apply (@map_is_mapt (S n)). Qed.
103
104
105 Lemma hlist'_impl {n A F G} (xs:tuple' A n)
106 : (hlist' (fun x => F x -> G x) xs) -> (hlist' F xs -> hlist' G xs).
107 Proof.
108 induction n; simpl in *; intuition.
109 Defined.
110
111 Lemma hlist_impl {n A F G} (xs:tuple A n)
112 : (hlist (fun x => F x -> G x) xs) -> (hlist F xs -> hlist G xs).
113 Proof.
114 destruct n; [ constructor | apply hlist'_impl ].
115 Defined.
116
117 Local Arguments Tuple.map : simpl never.
118 Lemma hlist_map {n A B F} {f:A -> B} (xs:tuple A n)
119 : hlist F (Tuple.map f xs) = hlist (fun x => F (f x)) xs.
120 Proof.
121 destruct n as [|n]; [ reflexivity | ].
122 induction n; [ reflexivity | ].
123 specialize (IHn (fst xs)).
124 destruct xs; rewrite Tuple.map_S.
125 simpl @hlist in *; rewrite <- IHn.
126 reflexivity.
127 Qed.
128
129 Lemma fold_right_and_True_hlist' {n A F} (xs:tuple' A n)
130 : iffT (List.fold_right and True (List.map F (Tuple.to_list' _ xs))) (hlist' F xs).
131 Proof.
132 induction n.
133 { simpl; tauto. }
134 { specialize (IHn (fst xs)).
135 destruct xs; simpl in *.
136 tauto. }
137 Qed.
138
139 Lemma fold_right_and_True_hlist {n A F} (xs:tuple A n)
140 : iffT (List.fold_right and True (List.map F (Tuple.to_list _ xs))) (hlist F xs).
141 Proof.
142 destruct n; [ simpl; tauto | apply fold_right_and_True_hlist' ].
143 Qed.
144
145 Global Instance mapt_Proper
146 : forall {n A F B},
147 Proper
148 ((forall_relation (fun x => pointwise_relation _ Logic.eq))
149 ==> forall_relation (fun ts => Logic.eq ==> Logic.eq))
150 (@mapt n A F B).
151 Proof.
152 intro n; unfold forall_relation, pointwise_relation, respectful.
153 repeat intro; subst; destruct n as [|n]; [ reflexivity | ].
154 induction n; simpl in *; congruence.
155 Qed.
156
157 Module Tuple.
158 Lemma map_id_ext {n A} (f : A -> A) (xs:tuple A n)
159 : hlist (fun x => f x = x) xs -> Tuple.map f xs = xs.
160 Proof.
161 destruct n as [|n]; [cbv in *; destruct xs; trivial|].
162 induction n as [|n IHn]; [cbv in *;trivial|].
163 simpl in *. destruct xs. simpl in *; intros [??].
164 rewrite map_S. eauto using f_equal2.
165 Qed.
166 End Tuple.