forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICCumulativity.v
248 lines (219 loc) · 7.92 KB
/
PCUICCumulativity.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(* Distributed under the terms of the MIT license. *)
From Equations Require Import Equations.
From Coq Require Import Bool String List Program BinPos Compare_dec Omega.
From MetaCoq.Template Require Import config utils AstUtils.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICLiftSubst PCUICUnivSubst PCUICEquality PCUICTyping PCUICWeakeningEnv
PCUICWeakening PCUICClosed PCUICReduction.
Require Import ssreflect ssrbool.
Require Import String.
From MetaCoq Require Import LibHypsNaming.
Local Open Scope string_scope.
Set Asymmetric Patterns.
Require Import CRelationClasses.
Require Import Equations.Type.Relation Equations.Type.Relation_Properties.
Reserved Notation " Σ ;;; Γ |- t == u " (at level 50, Γ, t, u at next level).
Lemma cumul_alt `{cf : checker_flags} Σ Γ t u :
Σ ;;; Γ |- t <= u <~> { v & { v' & (red Σ Γ t v * red Σ Γ u v' * leq_term (global_ext_constraints Σ) v v')%type } }.
Proof.
split.
{ induction 1. exists t, u. intuition auto; constructor.
destruct IHX as (v' & v'' & (redv & redv') & leqv).
exists v', v''. intuition auto. now eapply red_step.
destruct IHX as (v' & v'' & (redv & redv') & leqv).
exists v', v''. intuition auto. now eapply red_step. }
{ intros [v [v' [[redv redv'] Hleq]]]. apply red_alt in redv. apply red_alt in redv'.
apply clos_rt_rt1n in redv.
apply clos_rt_rt1n in redv'.
induction redv. induction redv'. constructor; auto.
econstructor 3; eauto.
econstructor 2; eauto. }
Qed.
Lemma cumul_refl' {cf : checker_flags} Σ Γ t : Σ ;;; Γ |- t <= t.
Proof.
eapply cumul_alt. exists t, t; intuition eauto. eapply leq_term_refl.
Qed.
Lemma conv_refl' {cf : checker_flags} Σ Γ t : Σ ;;; Γ |- t = t.
Proof.
split; apply cumul_refl'.
Qed.
Lemma red_cumul `{cf : checker_flags} {Σ : global_env_ext} {Γ t u} :
red Σ Γ t u ->
Σ ;;; Γ |- t <= u.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n in X.
induction X. apply cumul_refl'.
econstructor 2; eauto.
Qed.
Lemma red_cumul_inv `{cf : checker_flags} {Σ : global_env_ext} {Γ t u} :
red Σ Γ t u ->
Σ ;;; Γ |- u <= t.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n in X.
induction X. apply cumul_refl'.
econstructor 3; eauto.
Qed.
Lemma red_cumul_cumul `{cf : checker_flags} {Σ : global_env_ext} {Γ t u v} :
red Σ Γ t u -> Σ ;;; Γ |- u <= v -> Σ ;;; Γ |- t <= v.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n in X.
induction X. auto.
econstructor 2; eauto.
Qed.
Lemma red_cumul_cumul_inv `{cf : checker_flags} {Σ : global_env_ext} {Γ t u v} :
red Σ Γ t v -> Σ ;;; Γ |- u <= v -> Σ ;;; Γ |- u <= t.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n in X.
induction X. auto.
econstructor 3. eapply IHX. eauto. eauto.
Qed.
Lemma red_conv {cf:checker_flags} (Σ : global_env_ext) Γ t u : red Σ Γ t u -> Σ ;;; Γ |- t = u.
Proof.
intros. split; [apply red_cumul|apply red_cumul_inv]; auto.
Qed.
Lemma conv_cumul {cf:checker_flags} Σ Γ t u :
Σ ;;; Γ |- t = u -> (Σ ;;; Γ |- t <= u) * (Σ ;;; Γ |- u <= t).
Proof. trivial. Qed.
(* todo: move *)
Lemma All_All2_refl {A : Type} {R} {l : list A} : All (fun x : A => R x x) l -> All2 R l l.
Proof.
induction 1; constructor; auto.
Qed.
Lemma eq_term_App `{checker_flags} φ f f' :
eq_term φ f f' ->
isApp f = isApp f'.
Proof.
inversion 1; reflexivity.
Qed.
Lemma eq_term_mkApps `{checker_flags} φ f l f' l' :
eq_term φ f f' ->
All2 (eq_term φ) l l' ->
eq_term φ (mkApps f l) (mkApps f' l').
Proof.
induction l in l', f, f' |- *; intro e; inversion_clear 1.
- assumption.
- cbn. eapply IHl. constructor; assumption. assumption.
Qed.
Lemma leq_term_App `{checker_flags} φ f f' :
leq_term φ f f' ->
isApp f = isApp f'.
Proof.
inversion 1; reflexivity.
Qed.
Lemma leq_term_mkApps `{checker_flags} φ f l f' l' :
leq_term φ f f' ->
All2 (eq_term φ) l l' ->
leq_term φ (mkApps f l) (mkApps f' l').
Proof.
induction l in l', f, f' |- *; intro e; inversion_clear 1.
- assumption.
- cbn. apply IHl. constructor; try assumption. assumption.
Qed.
Inductive conv_alt `{checker_flags} (Σ : global_env_ext) (Γ : context) : term -> term -> Type :=
| conv_alt_refl t u : eq_term (global_ext_constraints Σ) t u -> Σ ;;; Γ |- t == u
| conv_alt_red_l t u v : red1 Σ Γ t v -> Σ ;;; Γ |- v == u -> Σ ;;; Γ |- t == u
| conv_alt_red_r t u v : Σ ;;; Γ |- t == v -> red1 (fst Σ) Γ u v -> Σ ;;; Γ |- t == u
where " Σ ;;; Γ |- t == u " := (@conv_alt _ Σ Γ t u) : type_scope.
Lemma red_conv_alt `{cf : checker_flags} Σ Γ t u :
red (fst Σ) Γ t u ->
Σ ;;; Γ |- t == u.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n_iff in X.
induction X. constructor. apply eq_term_refl.
apply clos_rt_rt1n_iff in X. apply red_alt in X.
econstructor 2; eauto.
Qed.
Hint Resolve red_conv_alt.
Hint Resolve leq_term_refl cumul_refl' : core.
Lemma red_conv_conv `{cf : checker_flags} Σ Γ t u v :
red (fst Σ) Γ t u -> Σ ;;; Γ |- u == v -> Σ ;;; Γ |- t == v.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n_iff in X.
induction X; auto.
econstructor 2; eauto.
Qed.
Lemma red_conv_conv_inv `{cf : checker_flags} Σ Γ t u v :
red (fst Σ) Γ t u -> Σ ;;; Γ |- v == u -> Σ ;;; Γ |- v == t.
Proof.
intros. apply red_alt in X. apply clos_rt_rt1n_iff in X.
induction X; auto.
now econstructor 3; [eapply IHX|]; eauto.
Qed.
Lemma conv_alt_sym `{cf : checker_flags} (Σ : global_env_ext) Γ t u :
wf Σ ->
Σ ;;; Γ |- t == u -> Σ ;;; Γ |- u == t.
Proof.
intros wfΣ X.
induction X.
- eapply eq_term_sym in e; now constructor.
- eapply red_conv_conv_inv. eapply red1_red in r. eauto. eauto.
- eapply red_conv_conv. eapply red1_red in r. eauto. eauto.
Qed.
Lemma conv_alt_red {cf : checker_flags} {Σ : global_env_ext} {Γ : context} {t u : term} :
Σ;;; Γ |- t == u <~> (∑ v v' : term, (red Σ Γ t v × red Σ Γ u v') × eq_term (global_ext_constraints Σ) v v').
Proof.
split. induction 1. exists t, u; intuition auto.
destruct IHX as [? [? [? ?]]].
exists x, x0; intuition auto. eapply red_step; eauto.
destruct IHX as [? [? [? ?]]].
exists x, x0; intuition auto. eapply red_step; eauto.
intros.
destruct X as [? [? [[? ?] ?]]].
eapply red_conv_conv; eauto.
eapply red_conv_conv_inv; eauto. now constructor.
Qed.
Lemma conv_alt_cumul `{cf : checker_flags} (Σ : global_env_ext) Γ t u : wf Σ ->
Σ ;;; Γ |- t == u -> Σ ;;; Γ |- t <= u.
Proof.
intros wfΣ H.
apply conv_alt_red in H as [v [v' [[l r] eq]]].
apply cumul_alt.
exists v, v'; intuition auto. now eapply eq_term_leq_term.
Qed.
Lemma conv_alt_conv `{cf : checker_flags} (Σ : global_env_ext) Γ t u : wf Σ ->
Σ ;;; Γ |- t == u -> Σ ;;; Γ |- t = u.
Proof.
intros wfΣ H. split. now apply conv_alt_cumul.
apply conv_alt_sym in H; auto. now apply conv_alt_cumul.
Qed.
Inductive conv_pb :=
| Conv
| Cumul.
Definition conv `{cf : checker_flags} leq Σ Γ u v :=
match leq with
| Conv => ∥ Σ ;;; Γ |- u == v ∥
| Cumul => ∥ Σ ;;; Γ |- u <= v ∥
end.
Lemma conv_conv_l `{cf : checker_flags} :
forall (Σ : global_env_ext) leq Γ u v, wf Σ ->
Σ ;;; Γ |- u == v ->
conv leq Σ Γ u v.
Proof.
intros Σ [] Γ u v h.
- cbn. constructor. assumption.
- cbn. constructor. now apply conv_alt_cumul.
Qed.
Lemma conv_conv_r `{cf : checker_flags} :
forall (Σ : global_env_ext) leq Γ u v, wf Σ ->
Σ ;;; Γ |- u == v ->
conv leq Σ Γ v u.
Proof.
intros Σ [] Γ u v wfΣ h.
- cbn. constructor. apply conv_alt_sym; auto.
- cbn. constructor. apply conv_alt_cumul. auto. now apply conv_alt_sym.
Qed.
Lemma cumul_App_l `{cf : checker_flags} :
forall {Σ Γ f g x},
Σ ;;; Γ |- f <= g ->
Σ ;;; Γ |- tApp f x <= tApp g x.
Proof.
intros Σ Γ f g x h.
induction h.
- eapply cumul_refl. constructor.
+ assumption.
+ apply eq_term_refl.
- eapply cumul_red_l ; try eassumption.
econstructor. assumption.
- eapply cumul_red_r ; try eassumption.
econstructor. assumption.
Qed.