Skip to content

Commit 3551ac8

Browse files
committed
feat: Add QGlobal
1 parent 9157f19 commit 3551ac8

File tree

11 files changed

+329
-78
lines changed

11 files changed

+329
-78
lines changed

common/theories/BasicAst.v

Lines changed: 226 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,154 @@ Inductive name : Set :=
1111
| nNamed (_ : ident).
1212
Derive NoConfusion EqDec for name.
1313

14+
Module QGlobal.
15+
16+
Record t := {
17+
library : dirpath ;
18+
id : ident
19+
}.
20+
21+
Notation make := Build_t.
22+
23+
Definition repr q := (q.(library), q.(id)).
24+
25+
Definition equal q1 q2 :=
26+
repr q1 == repr q2.
27+
28+
Definition compare q1 q2 :=
29+
let c := compare_ident q1.(id) q2.(id) in
30+
match c with
31+
| Lt | Gt => c
32+
| Eq => DirPathOT.compare q1.(library) q2.(library)
33+
end.
34+
35+
Definition to_string q :=
36+
string_of_dirpath q.(library) ^ "." ^ q.(id).
37+
38+
#[global, refine]
39+
Instance reflect_qglobal : ReflectEq QGlobal.t := {
40+
eqb := equal
41+
}.
42+
Proof.
43+
intros [lib1 id1] [lib2 id2]; unfold equal, repr; cbn.
44+
case: eqb_spec ; nodec.
45+
now constructor.
46+
Qed.
47+
48+
Lemma eqb_refl (g : t) : eqb g g = true.
49+
Proof.
50+
destruct g; cbn; apply eqb_refl.
51+
Qed.
52+
53+
#[global]
54+
Instance eq_dec_qglobal : EqDec t.
55+
Proof.
56+
intros q1 q2.
57+
destruct (q1 == q2) eqn:e.
58+
- apply eqb_eq in e. now left.
59+
- right. intros f. rewrite f in e. subst.
60+
rewrite eqb_refl in e. discriminate.
61+
Defined.
62+
63+
Inductive lt : t -> t -> Prop :=
64+
| ltIdQGlobal lib1 id1 lib2 id2 :
65+
IdentOT.lt id1 id2 ->
66+
lt (make lib1 id1) (make lib2 id2)
67+
| ltLibQGlobal id lib1 lib2 :
68+
DirPathOT.lt lib1 lib2 ->
69+
lt (make lib1 id) (make lib2 id).
70+
71+
Derive Signature for lt.
72+
73+
#[global] Instance lt_strorder : StrictOrder lt.
74+
Proof.
75+
constructor.
76+
- intros [lib id] X. inversion X; subst;
77+
now eapply irreflexivity in H0.
78+
- intros q1 q2 q3 H1 H2; inversion H1; inversion H2; subst; injection H5; intros; subst;
79+
try now constructor.
80+
* constructor; now etransitivity.
81+
* constructor; now etransitivity.
82+
Qed.
83+
84+
Definition ltb (q1 q2 : QGlobal.t) : bool :=
85+
match QGlobal.compare q1 q2 with
86+
| Lt => true
87+
| _ => false
88+
end.
89+
90+
Definition compare_spec : forall x y, CompareSpec (eq x y) (lt x y) (lt y x) (compare x y).
91+
Proof.
92+
intros [lib1 id1] [lib2 id2]. rewrite /compare. cbn. rewrite /compare_ident.
93+
destruct (IdentOT.compare_spec id1 id2) as [e | lt | gt]; subst.
94+
- destruct (DirPathOT.compare_spec lib1 lib2) as [e' | lt' | gt']; subst;
95+
now repeat constructor.
96+
- constructor. now constructor.
97+
- constructor. now constructor.
98+
Qed.
99+
100+
Lemma reflect_lt_qglobal (x y : t) : reflectProp (lt x y) (ltb x y).
101+
Proof.
102+
destruct (ltb x y) eqn:e; constructor; destruct x as [lib1 id1], y as [lib2 id2].
103+
- rewrite /ltb in e.
104+
destruct (compare_spec (make lib1 id1) (make lib2 id2)); try discriminate.
105+
assumption.
106+
- intro f. inversion f; subst.
107+
* rewrite /ltb /compare in e. rewrite /compare_ident in e. unfold IdentOT.lt in H0. rewrite H0 in e. discriminate.
108+
* rewrite /ltb /compare in e. rewrite /compare_ident in e. rewrite IdentOT.compare_refl in e. cbn in e. move/DirPathOT.compare_lt_lt: H0 => H0; rewrite H0 in e. discriminate.
109+
Qed.
110+
111+
Inductive le : t -> t -> Prop :=
112+
| leeqQGlobal q : le q q
113+
| leltQGlobal q1 q2 : lt q1 q2 -> le q1 q2.
114+
115+
Derive Signature for le.
116+
117+
#[global] Instance le_trans : Transitive le.
118+
Proof.
119+
intros [lib1 id1] [lib2 id2] [lib3 id3] H1 H2.
120+
inversion H1; inversion H2; subst; try now constructor.
121+
constructor. etransitivity; tea.
122+
Qed.
123+
124+
#[global] Instance le_preorder : PreOrder le.
125+
Proof.
126+
constructor.
127+
- intros [lib id]. constructor.
128+
- intros v1 v2 v3 X1 X2; inversion X1; inversion X2; subst; try now constructor.
129+
etransitivity; tea.
130+
Qed.
131+
132+
Definition leqb (q1 q2 : t) : bool :=
133+
(* (q1 == q2) || ltb q1 q2. *)
134+
match QGlobal.compare q1 q2 with
135+
| Gt => false
136+
| _ => true
137+
end.
138+
139+
Lemma reflect_le_qglobal (x y : t) : reflectProp (le x y) (leqb x y).
140+
Proof.
141+
destruct (leqb x y) eqn:e; constructor; destruct x as [lib1 id1], y as [lib2 id2].
142+
- rewrite /leqb in e. destruct (compare_spec (make lib1 id1) (make lib2 id2)); try discriminate.
143+
* injection H; intros; subst. now constructor.
144+
* now constructor.
145+
- intro f. inversion f; subst.
146+
* rewrite /leqb in e. destruct (compare_spec (make lib2 id2) (make lib2 id2)); try discriminate. now apply irreflexivity in H.
147+
* rewrite /leqb in e. destruct (compare_spec (make lib1 id1) (make lib2 id2)); try discriminate. eapply transitivity in H0. now apply irreflexivity in H0. assumption.
148+
Qed.
149+
150+
Definition leq_qglobal_dec v v' : {le v v'} + {~le v v'}.
151+
Proof.
152+
destruct v as [lib1 id1], v' as [lib2 id2].
153+
destruct (leqb (make lib1 id1) (make lib2 id2)) eqn:e.
154+
- now move/reflect_le_qglobal: e.
155+
- right; move/reflect_le_qglobal=> f; congruence.
156+
Defined.
157+
158+
End QGlobal.
159+
14160
Module QVar.
15-
Inductive repr_ {V : Set} := Var (_ : V).
161+
Inductive repr_ {V : Set} := Var (_ : V) | Global (_ : QGlobal.t).
16162
Derive NoConfusion for repr_.
17163
Arguments repr_ : clear implicits.
18164

@@ -24,36 +170,53 @@ Module QVar.
24170
Definition eqb {V : Set} `{ReflectEq V} (v1 v2 : repr_ V) : bool :=
25171
match v1, v2 with
26172
| Var i, Var j => eqb i j
173+
| Global q1, Global q2 => QGlobal.equal q1 q2
174+
| _, _ => false
27175
end.
28176

29177
Lemma eqb_refl {V : Set} `{ReflectEq V} (v : repr_ V) : eqb v v = true.
30178
Proof.
31-
destruct v. cbn. apply eqb_refl.
179+
destruct v; cbn; apply eqb_refl.
32180
Qed.
33181

34182
#[global, program] Instance reflect_eq_qvar {V : Set} `{ReflectEq V} : ReflectEq (repr_ V) :=
35183
{ eqb := eqb }.
36184
Next Obligation.
37-
destruct x, y; cbn; destruct (eqb_spec v v0); constructor.
38-
now f_equal. congruence.
185+
destruct x as [v1 | t1].
186+
- destruct y as [v2 | t2]; cbn; try case eqb_spec; nodec. now constructor.
187+
- destruct y as [v2 | t2]; cbn; try case eqb_spec; nodec.
188+
destruct t1 as [lib1 id1], t2 as [lib2 id2].
189+
unfold QGlobal.equal, QGlobal.repr; cbn.
190+
case: eqb_spec; nodec.
191+
now constructor.
39192
Qed.
40193

41-
#[global] Instance eq_dec_qvar {V : Set} `{EqDec V} : EqDec (repr_ V) := ltac:(intros v v'; decide equality).
194+
#[global] Instance eq_dec_qvar {V : Set} `{EqDec V} : EqDec (repr_ V).
195+
Proof.
196+
intros [v1 | t1] [v2 | t2]; cbn; try decide equality;
197+
(* Why is this instance not picked up by automation? *)
198+
eapply QGlobal.eq_dec_qglobal.
199+
Qed.
42200

43201
Inductive lt_ {V V_lt} : repr_ V -> repr_ V -> Prop :=
44-
| ltVarVar i j : V_lt i j -> lt_ (Var i) (Var j).
202+
| ltVarVar i j : V_lt i j -> lt_ (Var i) (Var j)
203+
| ltGlobalGlobal t1 t2 : QGlobal.lt t1 t2 -> lt_ (Global t1) (Global t2)
204+
| ltVarGlobal i q1 : lt_ (Var i) (Global q1).
205+
45206
Derive Signature for lt_.
46207
Arguments lt_ {V} V_lt.
47208

48209
Definition lt := lt_ Nat.lt.
210+
49211
#[global] Instance lt_strorder : StrictOrder lt.
50212
Proof.
51213
constructor.
52-
- intros v X; inversion X.
214+
- intros v X; inversion X;
53215
now eapply irreflexivity in H1.
54-
- intros v1 v2 v3 X1 X2;
55-
inversion X1; inversion X2; constructor.
56-
subst. etransitivity; tea. inversion H3. now subst.
216+
- intros v1 v2 v3 X1 X2.
217+
inversion X1; inversion X2; subst; try congruence; try now constructor.
218+
* inversion H3; subst. constructor. now etransitivity; tea.
219+
* inversion H3; subst. constructor. now etransitivity; tea.
57220
Qed.
58221

59222
Definition lt_compat : Proper (eq ==> eq ==> iff) lt.
@@ -64,32 +227,41 @@ Module QVar.
64227
Definition ltb_ {V V_ltb} (v1 v2 : repr_ V) : bool :=
65228
match v1, v2 with
66229
| Var i, Var j => V_ltb i j
230+
| Global t1, Global t2 => QGlobal.ltb t1 t2
231+
| Var _, _ => true
232+
| _, _ => false
67233
end.
234+
68235
Arguments ltb_ {V} V_ltb.
69236

70237
Definition ltb := ltb_ Nat.ltb.
71238

72239
Lemma reflect_lt_qvar {V : Set} (x y : t) : reflectProp (lt x y) (ltb x y).
73240
Proof.
74-
destruct (ltb x y) eqn:e; constructor; destruct x, y; rewrite /ltb /ltb_ in e.
241+
destruct (ltb x y) eqn:e; constructor; destruct x as [n1 | [lib1 id1]], y as [n2 | [lib2 id2]]; rewrite /ltb /ltb_ in e; try congruence; try now (intro f; inversion f).
75242
- constructor. now apply Nat.ltb_lt.
76-
- intro f. inversion f. subst. apply Nat.ltb_lt in H1. congruence.
243+
- now constructor.
244+
- constructor. apply (QGlobal.reflect_lt_qglobal _ _ e).
245+
- intro f; inversion f; subst.
246+
move/QGlobal.reflect_lt_qglobal: H1. congruence.
77247
Qed.
78248

79249
Inductive le_ {V V_le} : repr_ V -> repr_ V -> Prop :=
80-
| leVarVar i j : V_le i j -> le_ (Var i) (Var j).
250+
| leVarVar i j : V_le i j -> le_ (Var i) (Var j)
251+
| leGlobalGlobal t1 t2 : QGlobal.le t1 t2 -> le_ (Global t1) (Global t2)
252+
| leVarGlobal i q1 : le_ (Var i) (Global q1).
81253
Derive Signature for le_.
82254
Arguments le_ {V} V_le.
83255

84256
Definition le := le_ Nat.le.
85-
257+
86258
#[global] Instance le_preorder : PreOrder le.
87259
Proof.
88260
constructor.
89-
- intros i. destruct i. constructor. apply le_n.
90-
- intros v1 v2 v3 X1 X2; inversion X1; inversion X2; constructor.
91-
subst. etransitivity; tea. inversion H3. now subst.
92-
Qed.
261+
- intros i. destruct i; constructor; constructor.
262+
- intros v1 v2 v3 X1 X2; inversion X1; inversion X2; subst; try discriminate; constructor;
263+
now etransitivity; tea.
264+
Qed.
93265

94266
Definition le_compat : Proper (eq ==> eq ==> iff) le.
95267
Proof.
@@ -99,38 +271,49 @@ Module QVar.
99271
Definition leqb_ {V V_leqb} (v1 v2 : repr_ V) : bool :=
100272
match v1, v2 with
101273
| Var i, Var j => V_leqb i j
274+
| Global t1, Global t2 => QGlobal.leqb t1 t2
275+
| Var _, _ => true
276+
| _, _ => false
102277
end.
103278
Arguments leqb_ {V} V_leqb.
104279

105280
Definition leqb := leqb_ Nat.leb.
106281

107282
Lemma reflect_le_qvar {V : Set} (x y : t) : reflectProp (le x y) (leqb x y).
108283
Proof.
109-
destruct (leqb x y) eqn:e; constructor; destruct x, y; rewrite /leqb /leqb_ in e.
284+
destruct (leqb x y) eqn:e; constructor; destruct x, y; rewrite /leqb /leqb_ in e; try discriminate; try now (intro f; inversion f).
110285
- constructor. now apply Nat.leb_le.
111-
- intro f. inversion f. subst. apply Nat.leb_le in H1. congruence.
286+
- now constructor.
287+
- constructor. apply (QGlobal.reflect_le_qglobal _ _ e).
288+
- intro f. inversion f; subst. move/QGlobal.reflect_le_qglobal: H1. congruence.
112289
Qed.
113290

114291
Definition compare (v1 v2 : t) : comparison
115292
:= match v1, v2 with
116293
| Var i, Var j => Nat.compare i j
294+
| Global t1, Global t2 => QGlobal.compare t1 t2
295+
| Var _, _ => Lt
296+
| _, Var _ => Gt
117297
end.
118298

119299
Lemma compare_spec x y : CompareSpec (eq x y) (lt x y) (lt y x) (compare x y).
120300
Proof.
121301
cbv [compare].
122-
destruct x, y.
302+
destruct x as [i | q1], y as [j | q2].
123303
all: lazymatch goal with
124304
| [ |- context[Nat.compare ?x ?y] ]
125305
=> destruct (Nat.compare_spec x y)
126306
| _ => idtac
127307
end.
128308
all: repeat constructor; try apply f_equal; try assumption.
309+
destruct (QGlobal.compare_spec q1 q2); now (repeat constructor).
129310
Qed.
130311

131312
Definition eq_ {V} eq_V (q1 q2 : repr_ V) : Prop :=
132313
match q1, q2 with
133314
| Var q1, Var q2 => eq_V q1 q2
315+
| Global t1, Global t2 => t1 = t2
316+
| _, _ => False
134317
end.
135318

136319
Definition eq : t -> t -> Prop := eq_ (fun n m => n = m).
@@ -145,47 +328,56 @@ Module QVar.
145328

146329
Definition eq_dec_ {V : Set} {eq_V} (eq_dec_V : forall v1 v2 : V, {eq_V v1 v2} + {~ (eq_V v1 v2)})
147330
(v1 v2 : repr_ V) : {eq_ eq_V v1 v2} + {~ (eq_ eq_V v1 v2)}.
148-
Proof. destruct v1, v2. unfold eq_. apply eq_dec_V. Defined.
331+
Proof. destruct v1, v2; unfold eq_; try tauto.
332+
- apply eq_dec_V.
333+
- apply QGlobal.eq_dec_qglobal.
334+
Defined.
149335

150336
Definition eq_dec := eq_dec_ Nat.eq_dec.
151337

152-
Definition sup_ {V} V_sup (x y : repr_ V) : repr_ V :=
338+
(* Definition sup_ {V} V_sup (x y : repr_ V) : repr_ V :=
153339
match x, y with
154340
| Var n, Var m => Var (V_sup n m)
155-
end.
341+
end. *)
156342

157-
Definition sup : t -> t -> t := sup_ Nat.max.
343+
(* Definition sup : t -> t -> t := sup_ Nat.max. *)
158344

159-
Lemma sup_comm_ {V : Set} V_sup :
345+
(* Lemma sup_comm_ {V : Set} V_sup :
160346
(forall n m : V, V_sup n m = V_sup m n) ->
161347
forall (x y : repr_ V), sup_ V_sup x y = sup_ V_sup y x.
162348
Proof.
163349
intros Vcomm ??. destruct x, y; cbn.
164350
f_equal. apply Vcomm.
165-
Qed.
351+
Qed. *)
166352

167-
Lemma sup_comm (x y : t) : sup x y = sup y x.
353+
(* Lemma sup_comm (x y : t) : sup x y = sup y x.
168354
Proof.
169355
apply sup_comm_, Nat.max_comm.
170-
Qed.
356+
Qed. *)
171357

172-
Lemma sup_idem (x : t) : sup x x = x.
358+
(* Lemma sup_idem (x : t) : sup x x = x.
173359
Proof.
174360
destruct x; cbn. now rewrite Nat.max_id.
175-
Qed.
361+
Qed. *)
176362
End QVar.
177363

178364
Definition leq_qvar_dec v v' : {QVar.le v v'} + {~QVar.le v v'}.
179365
Proof.
180-
destruct v, v'. cbv[QVar.le].
181-
destruct (le_dec n n0).
366+
destruct v as [i | q1], v' as [j | q2]; cbv[QVar.le].
367+
- destruct (le_dec i j).
368+
* left. now constructor.
369+
* right. intro. inversion H. now apply n.
182370
- left. now constructor.
183-
- right. intro. inversion H. now apply n1.
371+
- right. intro. inversion H.
372+
- destruct (QGlobal.leq_qglobal_dec q1 q2) as [Hle | Hnle].
373+
* now left; constructor.
374+
* right. intro f. apply Hnle. now inversion f.
184375
Qed.
185376

186377
Definition string_of_qvar (q : QVar.t) :=
187378
match q with
188379
| QVar.Var i => "α" ^ string_of_nat i
380+
| QVar.Global q => "γ" ^ QGlobal.to_string q
189381
end.
190382

191383
Inductive relevance : Set := Relevant | Irrelevant | RelevanceVar (_ : QVar.t).

0 commit comments

Comments
 (0)