-
Notifications
You must be signed in to change notification settings - Fork 10
/
Prelude.v
316 lines (249 loc) · 7.93 KB
/
Prelude.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
(* Global utility *)
(* Partly stolen from MetaCoq *)
From Coq Require Import Utf8 Lia.
Set Warnings "-notation-overridden".
From mathcomp Require Import ssreflect eqtype ssrbool ssrnat.
Set Warnings "notation-overridden".
From HB Require Import structures.
From extructures Require Import ord fset.
From Equations Require Import Equations.
From SSProve.Mon Require SPropBase.
Set Bullet Behavior "Strict Subproofs".
Set Default Goal Selector "!".
Set Primitive Projections.
(* Simple products *)
(* Notation "x × y" := (prod x y) (at level 80, right associativity). *)
(* Dependent sums *)
(* Use \sum to input ∑ in Company Coq (it is not a sigma Σ). *)
Notation "'∑' x .. y , p" := (sigT (fun x => .. (sigT (fun y => p%type)) ..))
(at level 200, x binder, right associativity,
format "'[' '∑' '/ ' x .. y , '/ ' p ']'")
: type_scope.
Notation "( x ; y )" := (@existT _ _ x y).
Notation "( x ; y ; z )" := (x ; ( y ; z)).
Notation "( x ; y ; z ; t )" := (x ; ( y ; (z ; t))).
Notation "( x ; y ; z ; t ; u )" := (x ; ( y ; (z ; (t ; u)))).
Notation "( x ; y ; z ; t ; u ; v )" := (x ; ( y ; (z ; (t ; (u ; v))))).
Notation "x .π1" := (@projT1 _ _ x) (at level 3, format "x '.π1'").
Notation "x .π2" := (@projT2 _ _ x) (at level 3, format "x '.π2'").
(* Handy rewrite on sig *)
Lemma sig_rewrite_aux :
∀ {T A} {P : A → Prop} {x y} (p : T → A) (h : P (p x)) (e : x = y),
P (p y).
Proof.
intros T A P x y p h e. subst. auto.
Defined.
Lemma sig_rewrite :
∀ {T A} {P : A → Prop} {x y} (p : T → A) (h : P (p x)) (e : x = y),
exist _ (p x) h = exist _ (p y) (sig_rewrite_aux p h e).
Proof.
intros T A P x y p h e. subst. reflexivity.
Qed.
Ltac sig_rewrite e :=
lazymatch type of e with
| ?x = _ =>
match goal with
| |- context [ exist ?P ?p ?h ] =>
lazymatch p with
| context [ x ] =>
lazymatch eval pattern x in p with
| (fun x => @?q x) ?y =>
erewrite (sig_rewrite q _ e)
end
end
end
end.
(** Tactic sig rewrite
Usage: you have e : x = y as an hypothesis and you want to rewrite e inside
a term of the form exist _ u v, specifically inside the term u.
sig rewrite e will replace x by y in u and update v accordingly.
*)
Tactic Notation "sig" "rewrite" hyp(e) :=
sig_rewrite e.
(** Tactic falso
Usage: you have an hyothesis containing a use of [False_rect] at top-level.
This tactic will take it to close the goal.
*)
Ltac falso :=
lazymatch goal with
| |- context [ False_rect _ ?x] => exact (False_rect _ x)
| h : context [ False_rect _ ?x ] |- _ => exact (False_rect _ x)
end.
(** mathcomp: derive EqDec for any ordType *)
#[export] Instance ordType_EqDec {A : ordType} : EqDec A.
Proof.
intros x y. destruct (x == y) eqn:e.
- move: e => /eqP. auto.
- move: e => /eqP. auto.
Defined.
(** Notion of positive natural number
Usage: Simply write [mkpos n] to turn [n] into a positive natural number.
The positivity proof should be inferred by the [lia] tactic or some other
means.
*)
Class Positive (n : nat) : Prop :=
is_positive : 0 < n.
Ltac nat_reify :=
repeat match goal with
| h : is_true (_ < _) |- _ => move: h => /ltP h
| h : is_true (_ <= _) |- _ => move: h => /leqP h
| h : is_true (_ == _) |- _ => move: h => /eqP h
end.
#[export] Hint Extern 1 (Positive ?n) =>
reflexivity : typeclass_instances.
#[export] Hint Extern 2 (Positive ?n) =>
unfold Positive ; apply/ltP ; lia : typeclass_instances.
#[export] Hint Extern 4 (Positive ?n) =>
unfold Positive ; apply/ltP ; nat_reify ; lia : typeclass_instances.
#[export] Instance PositiveExp2 n : Positive (2^n)%N.
Proof.
unfold Positive. apply/ltP. induction n.
- auto.
- rewrite expnS. rewrite mulSnr. rewrite mulSnr.
change (0 * ?n) with 0.
set (m := 2^n) in *. clearbody m. cbn.
rewrite -?addnE. rewrite -plusE.
lia.
Qed.
Lemma Positive_prod :
∀ {n m},
Positive n →
Positive m →
Positive (n * m).
Proof.
intros n m hn hm.
unfold Positive in *.
eapply leq_trans. 2: eapply leq_pmull. all: auto.
Qed.
(* Instance Positive_prod {n m} `{Positive n} `{Positive m} :
Positive (n * m).
Proof.
unfold Positive in *.
eapply leq_trans. 2: eapply leq_pmull. all: auto.
Qed. *)
#[export] Hint Extern 2 (Positive (?n * ?m)) =>
eapply Positive_prod : typeclass_instances.
Record positive := mkpos {
pos : nat ;
cond_pos : Positive pos
}.
Arguments mkpos _ {_}.
Coercion pos : positive >-> nat.
#[export] Hint Extern 1 (Positive ?n.(pos)) =>
eapply cond_pos
: typeclass_instances.
Definition positive_eq : rel positive :=
λ u v, u.(pos) == v.(pos).
Lemma positive_eqP : Equality.axiom positive_eq.
Proof.
intros [n hn] [m hm]. unfold positive_eq. simpl.
destruct (n == m) eqn:e.
- move: e => /eqP e. subst. left.
f_equal. apply eq_irrelevance.
- move: e => /eqP e. right.
intro h. apply e. inversion h. reflexivity.
Qed.
HB.instance Definition _ := hasDecEq.Build _ positive_eqP.
(** Lt class, for finite types *)
Class Lt n m :=
is_in_fin : n < m.
#[export] Hint Extern 1 (Lt ?n ?m) =>
reflexivity : typeclass_instances.
#[export] Hint Extern 2 (Lt ?n ?m) =>
unfold Lt ; apply/ltP ; lia : typeclass_instances.
#[export] Hint Extern 4 (Lt ?n) =>
unfold Lt ; apply/ltP ; nat_reify ; lia : typeclass_instances.
#[export] Instance Positive_Lt n `{h : Positive n} : Lt 0 n.
Proof.
auto.
Qed.
Definition PositiveInFin n m (h : Lt n m) : Positive m.
Proof.
unfold Lt in h. exact _.
Qed.
(* We use a hint to avoid a loop with Positive_Lt *)
#[export] Hint Extern 8 (Positive ?m) =>
match goal with
| h : Lt ?n m |- _ => exact (PositiveInFin n m h)
end
: typeclass_instances.
Lemma positive_ext :
∀ (p q : positive),
p.(pos) = q.(pos) →
p = q.
Proof.
intros [p hp] [q hq] e.
cbn in e. subst.
f_equal. apply eq_irrelevance.
Qed.
(** Tactic to unfold all positives (NEEDED?) *)
Ltac unfold_positives :=
repeat match goal with
| p : positive |- _ =>
let n := fresh "p" in
let h := fresh "h" in
destruct p as [n h] ;
repeat change (pos {| pos := n ; cond_pos := h |}) with n in *
end.
#[export] Instance PositiveEqDec n : EqDec (Positive n).
Proof.
left. apply eq_irrelevance.
Qed.
Derive NoConfusion NoConfusionHom for positive.
(* Utility for defining functions with Equations *)
Definition inspect {A : Type} (x : A) : { y : A | y = x } :=
exist _ x Logic.eq_refl.
(** Hints notation
When dealing with typeclasses, sometimes automation will fail.
The purpose of this is to be able to help the automation by providing
hints without having to write the whole term.
[hints h1 ; .. ; hn ] will try to solve the goal by having h1 to hn in the
context. This can be useful to provide lemmata that are not usually picked
up by the instance mechanism.
This could also be used in combination with Programs or Equations'
automation.
[hints] is also provided for completeness, but is merely long for _.
*)
Declare Scope package_scope.
Delimit Scope package_scope with pack.
(* Hints notation *)
Notation "[ 'hints' ]" :=
(_)
(at level 0, only parsing)
: package_scope.
Notation "[ 'hints' x1 ]" :=
(let hint := x1 in _)
(at level 0, only parsing)
: package_scope.
Notation "[ 'hints' x ; .. ; z ]" :=
(let hint := x in .. (let hint := z in _) ..)
(at level 0, only parsing)
: package_scope.
(* Tactics to deal with \in fset *)
Ltac in_fset_auto :=
rewrite in_fset ; reflexivity.
(* Succeeds for x \in S if S contains syntactically x, S seq *)
Ltac inseq_try :=
apply/orP ; first [
left ; apply/eqP ; reflexivity
| right ; inseq_try
].
Ltac inset_try :=
rewrite in_fset ; inseq_try.
Ltac auto_in_fset :=
eauto ;
try in_fset_auto ;
try inset_try.
(* TODO Same as finmap.oextract but with a better name? *)
Definition getSome {A} (o : option A) :
isSome o → A.
Proof.
intro h.
destruct o. 2: discriminate.
assumption.
Defined.
Definition testSome {A} (P : A → bool) (o : option A) : bool :=
match o with
| Some a => P a
| None => false
end.