-
Notifications
You must be signed in to change notification settings - Fork 1
/
TODOProof.v
598 lines (540 loc) · 16.2 KB
/
TODOProof.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
Require Import vellvm.
Require Import sflib.
Require Import memory_props.
Require Import TODO.
Import Opsem.
Require Import Classical.
Set Implicit Arguments.
Ltac des_outest_ifsG :=
match goal with
| |- context[ match ?x with _ => _ end ] =>
match (type of x) with
| { _ } + { _ } => destruct x; clarify
| _ => let Heq := fresh "Heq" in destruct x as [] eqn: Heq; clarify
end
end
.
Ltac hide_goal :=
match goal with
| [ |- ?G ] => let name := fresh "HIDDEN_GOAL" in
set (name := G); replace G with name by reflexivity; move name at top
end.
(* TODO: Is it replacable by some lemma in stdlib? or tactic? *)
Lemma dependent_split
(A B: Prop)
(HYPA: A)
(HYPB: <<HYPA: A>> -> B)
:
<<GOAL: A /\ B>>
.
Proof.
split; ss.
apply HYPB; ss.
Qed.
Lemma Pos_lt_le_irrefl
a b
(LE: (a <= b)%positive)
(LT: (b < a)%positive)
:
False
.
Proof. eapply Pos.lt_irrefl. eapply Pos.lt_le_trans; eauto. Qed.
Lemma Forall_harder
A
(P Q: A -> Prop)
l
(FORALL: List.Forall P l)
(HARDER: forall a, P a -> Q a)
:
<<FORALL: List.Forall Q l>>
.
Proof.
ginduction l; ii; ss.
inv FORALL. econs; eauto.
eapply IHl; eauto.
Qed.
Ltac reductio_ad_absurdum :=
match goal with
| [ |- ?G ] => destruct (classic G) as [tmp | REDUCTIO_AD_ABSURDUM];
[assumption|exfalso]
end
.
Ltac exists_prop PROP :=
tryif
(repeat multimatch goal with
| [H: PROP |- _ ] => (* idtac "Found!"; idtac H; *) fail 2
end)
then fail
else idtac
.
(* get equality's transitive closure *)
(* TODO: it checks equal to strictly; "(0, 1).fst != 0" here. *)
Ltac eq_closure_tac :=
repeat
(repeat multimatch goal with
| [H1: ?A = ?B, H2: ?B = ?C |- _ ] =>
(* idtac "------------------------"; *)
(* idtac H1; idtac H2; *)
tryif (check_equal A C)
then (* idtac "FAILREFL1"; *) fail
else
tryif (exists_prop (A = C) + exists_prop (C = A))
then (* idtac "FAILREFL2" *) idtac
else
let name := fresh "EQ_CLOSURE_TAC" in
exploit eq_trans; [exact H1|exact H2|]; intro name
| [H1: ?B = ?A, H2: ?B = ?C |- _ ] =>
tryif (check_equal A C)
then (* idtac "FAILREFL1"; *) fail
else
tryif (exists_prop (A = C) + exists_prop (C = A))
then (* idtac "FAILREFL2" *) idtac
else
let name := fresh "EQ_CLOSURE_TAC" in
exploit eq_trans; [exact (eq_sym H1)|exact H2|]; intro name
end)
.
(* COPIED FROM https://www.cis.upenn.edu/~bcpierce/sf/current/LibTactics.html *)
(* TODO: is it OK? *)
(* TODO: move to proper position; I think sflib should be OK *)
(* TODO: also import some other good things, e.g. gens *)
Tactic Notation "clears" ident(X1) :=
let rec doit _ :=
match goal with
| H:context[X1] |- _ => clear H; try (doit tt)
| _ => clear X1
end in doit tt.
Tactic Notation "clears" ident(X1) ident(X2) :=
clears X1; clears X2.
Tactic Notation "clears" ident(X1) ident(X2) ident(X3) :=
clears X1; clears X2; clears X3.
Tactic Notation "clears" ident(X1) ident(X2) ident(X3) ident(X4) :=
clears X1; clears X2; clears X3; clears X4.
Tactic Notation "clears" ident(X1) ident(X2) ident(X3) ident(X4)
ident(X5) :=
clears X1; clears X2; clears X3; clears X4; clears X5.
Tactic Notation "clears" ident(X1) ident(X2) ident(X3) ident(X4)
ident(X5) ident(X6) :=
clears X1; clears X2; clears X3; clears X4; clears X5; clears X6.
(* TODO: This should fail when ident appears in the goal! *)
(* It currently succeeds, only removing preemises *)
Ltac rewrite_everywhere H := rewrite H in *.
Ltac all_with_term TAC TERM :=
repeat multimatch goal with
| H: context[TERM] |- _ => TAC H
end
.
Ltac clear_unused :=
repeat multimatch goal with
| [H: ?T |- _] =>
match (type of T) with
| Prop => idtac
| _ => try clear H
end
end
.
Ltac clear_tautology :=
repeat match goal with
| [H: ?A = ?B, H2: ?B = ?A |- _] => clear H2
| [H: True |- _] => clear H
| [H: ?X, H2: ?X |- _] => clear H2
| [H: ?A = ?A |- _] => clear H
end
.
Ltac clear_tac := repeat (clear_unused; clear_tautology).
Ltac des_outest_ifs H :=
match goal with
| H': context[ match ?x with _ => _ end ] |- _ =>
check_equal H' H;
match (type of x) with
| { _ } + { _ } => destruct x; clarify
| _ => let Heq := fresh "Heq" in destruct x as [] eqn: Heq; clarify
end
end.
Ltac des_ifs_safe_aux TAC :=
TAC;
repeat
multimatch goal with
| |- context[match ?x with _ => _ end] =>
match (type of x) with
| { _ } + { _ } => destruct x; TAC; []
| _ => let Heq := fresh "Heq" in destruct x as [] eqn: Heq; TAC; []
end
| H: context[ match ?x with _ => _ end ] |- _ =>
match (type of x) with
| { _ } + { _ } => destruct x; TAC; []
| _ => let Heq := fresh "Heq" in destruct x as [] eqn: Heq; TAC; []
end
end.
Tactic Notation "des_ifs_safe" := des_ifs_safe_aux clarify.
Tactic Notation "des_ifs_safe" tactic(TAC) := des_ifs_safe_aux TAC.
Ltac abstr_aux x var_name :=
let hyp_name := fresh "abstr_hyp_name" in
remember x as var_name eqn:hyp_name; clear hyp_name
.
Tactic Notation "abstr" constr(H) := let var_name := fresh "abstr_var_name" in abstr_aux H var_name.
Tactic Notation "abstr" constr(H) ident(var_name) := abstr_aux H var_name.
Example abstr_demo: (1 + 2 = 3) -> False.
i.
abstr_aux (1 + 2) my_name. Undo 1.
abstr (1 + 2).
Abort.
Ltac des_ifsH H :=
clarify;
repeat
match goal with
| H': context[ match ?x with _ => _ end ] |- _ =>
check_equal H' H;
match (type of x) with
| { _ } + { _ } => destruct x; clarify
| _ => let Heq := fresh "Heq" in destruct x as [] eqn: Heq; clarify
end
end.
Ltac des_ifsG :=
clarify;
repeat
match goal with
| |- context[match ?x with _ => _ end] =>
match (type of x) with
| { _ } + { _ } => destruct x; clarify
| _ => let Heq := fresh "Heq" in destruct x as [] eqn: Heq; clarify
end
end.
Ltac expl_aux H TAC :=
let n := fresh H in
(* one goal or zero goal *)
first[exploit H; TAC; []; repeat intro n; des|
exploit H; TAC; fail]
.
Tactic Notation "expl" constr(H) := expl_aux H eauto.
Tactic Notation "expl" constr(H) tactic(TAC) := expl_aux H TAC.
(* multimatch is needed for "solve [all inv]" *)
Ltac all TAC :=
repeat multimatch goal with
| H: _ |- _ => TAC H
end
.
Ltac apply_all x := all (ltac:(apply_in) x).
Lemma list_disjoint_cons_inv
X (hd: X) tl xs
(DISJOINT: list_disjoint (hd :: tl) xs)
:
<<DISJOINT: list_disjoint [hd] xs /\ list_disjoint tl xs>>
.
Proof.
splits.
- ii. clarify. ss. des; ss. clarify. expl DISJOINT. left. ss.
- eapply list_disjoint_cons_left; eauto.
Qed.
Lemma f_equal6 (A1 A2 A3 A4 A5 A6 B: Type) (f: A1 -> A2 -> A3 -> A4 -> A5 -> A6 -> B)
(x1 y1: A1) (EQ1: x1 = y1)
(x2 y2: A2) (EQ2: x2 = y2)
(x3 y3: A3) (EQ3: x3 = y3)
(x4 y4: A4) (EQ4: x4 = y4)
(x5 y5: A5) (EQ5: x5 = y5)
(x6 y6: A6) (EQ6: x6 = y6)
:
<<EQ: f x1 x2 x3 x4 x5 x6 = f y1 y2 y3 y4 y5 y6>>
.
Proof. subst. reflexivity. Qed.
Lemma f_equal7 (A1 A2 A3 A4 A5 A6 A7 B: Type) (f: A1 -> A2 -> A3 -> A4 -> A5 -> A6 -> A7 -> B)
(x1 y1: A1) (EQ1: x1 = y1)
(x2 y2: A2) (EQ2: x2 = y2)
(x3 y3: A3) (EQ3: x3 = y3)
(x4 y4: A4) (EQ4: x4 = y4)
(x5 y5: A5) (EQ5: x5 = y5)
(x6 y6: A6) (EQ6: x6 = y6)
(x7 y7: A7) (EQ7: x7 = y7)
:
<<EQ: f x1 x2 x3 x4 x5 x6 x7 = f y1 y2 y3 y4 y5 y6 y7>>
.
Proof. subst. reflexivity. Qed.
Lemma f_equal8 (A1 A2 A3 A4 A5 A6 A7 A8 B: Type) (f: A1 -> A2 -> A3 -> A4 -> A5 -> A6 -> A7 -> A8 -> B)
(x1 y1: A1) (EQ1: x1 = y1)
(x2 y2: A2) (EQ2: x2 = y2)
(x3 y3: A3) (EQ3: x3 = y3)
(x4 y4: A4) (EQ4: x4 = y4)
(x5 y5: A5) (EQ5: x5 = y5)
(x6 y6: A6) (EQ6: x6 = y6)
(x7 y7: A7) (EQ7: x7 = y7)
(x8 y8: A8) (EQ8: x8 = y8)
:
<<EQ: f x1 x2 x3 x4 x5 x6 x7 x8 = f y1 y2 y3 y4 y5 y6 y7 y8>>
.
Proof. subst. reflexivity. Qed.
Ltac rpapply_raw H :=
first[erewrite f_equal8 | erewrite f_equal7 | erewrite f_equal6 | erewrite f_equal5 |
erewrite f_equal4 | erewrite f_equal3 | erewrite f_equal2 | erewrite f_equal];
[eapply H|..]; try reflexivity.
Ltac is_applied_function TARGET :=
match TARGET with
| ?f ?x =>
idtac
| _ => fail
end
.
Ltac has_inside_strict A B :=
match A with
| context[B] => tryif (check_equal A B) then fail else idtac
| _ => fail
end
.
Ltac is_inside_others_body TARGET :=
tryif (repeat multimatch goal with
| [ |- context[?f ?x] ] =>
(* idtac f; idtac x; *)
tryif (has_inside_strict x TARGET)
then fail 2
else fail
end)
then fail
else idtac
.
Ltac on_leftest_function TAC :=
(* repeat *)
multimatch goal with
| [ |- context[?f ?x] ] =>
tryif (is_applied_function f)
then fail
else
tryif (is_inside_others_body f)
then fail
else TAC f
(* else TAC constr:(f) *)
(* TODO: What is the difference? *)
end
.
(* TODO: more cannonical way to get leftest function? *)
(* I tried match reverse but it was not good *)
(* TODO: I want to define "get_leftest_function" *)
(* TODO: try tactic notation ? *)
Ltac leftest_rpapply H :=
on_leftest_function ltac:(fun f =>
(idtac f; first
(* TODO: why rewrite "with" doesn't work? *)
[ erewrite (f_equal8 f)
| erewrite (f_equal7 f)
| erewrite (f_equal6 f)
| erewrite (f_equal5 f)
| erewrite (f_equal4 f)
| erewrite (f_equal3 f)
| erewrite (f_equal2 f)
| erewrite (f_equal f) | fail]); [ eapply H | .. ]; try reflexivity)
.
Ltac is_type x :=
match type of x with
| Type => idtac
| Set => idtac
| Prop => idtac (* TODO: needed? *)
| _ => fail
end.
Ltac is_term_applied_function TARGET :=
match TARGET with
| ?f ?x =>
tryif (is_type x) then fail else idtac
| _ => fail
end
.
Ltac on_leftest_function_with_type TAC :=
(* repeat *)
multimatch goal with
| [ |- context[?f ?x] ] =>
tryif (is_term_applied_function f)
then fail
else
tryif (is_inside_others_body f)
then fail
else TAC f
end
.
Ltac rpapply H :=
on_leftest_function_with_type ltac:(fun f =>
(idtac f; first
(* TODO: why rewrite "with" doesn't work? *)
[ erewrite (f_equal8 f)
| erewrite (f_equal7 f)
| erewrite (f_equal6 f)
| erewrite (f_equal5 f)
| erewrite (f_equal4 f)
| erewrite (f_equal3 f)
| erewrite (f_equal2 f)
| erewrite (f_equal f) | fail]); [ eapply H | .. ]; try reflexivity)
.
Goal forall a b,
let weird_func1 x y z w := (x + y + z + w) in
let weird_func2 x y := (x * y) > 0 in
(weird_func2 (weird_func1 1 1 1 1) (b+a)) ->
(weird_func2 (weird_func1 1 1 1 1) (a+b))
.
Proof.
i. subst weird_func1. subst weird_func2.
abstr (fun x y : Z => x * y > 0) weird_func1.
Fail rpapply_raw H.
erewrite f_equal4. Undo 1. (* rpapply_raw tries in decreasing order. *)
(* f_equal4 and then f_equal3 *)
erewrite f_equal3. Undo 1. (* Anyhow, that doesn't matter now . . . *)
on_leftest_function ltac:(fun x => idtac x).
leftest_rpapply H. Undo 1.
rpapply H. Undo 1.
Undo 3.
Fail erewrite (f_equal2 (fun x y : Z => x * y > 0)).
Fail erewrite f_equal2 with (f:= (fun x y : Z => x * y > 0)).
(* TODO: without "abstr", below two also fails... *)
(* It seems some kind of Coqs default reduction mechanism is always on, even in rewriting *)
(* It may make sense to assume, our proof status has already undergone that reduction. *)
(* For example, simpl in * will do reduction here, more than needed *)
simpl in *. Undo 1.
(* If this becomes problem in actual proof, we may add "remember" & "subst" in rpapply *)
Abort.
Goal (Forall (fun x => x > 0)%nat [1 ; 2]%nat) ->
(Forall (fun x => x+1 > 1)%nat [1+0 ; 0+2]%nat)
.
Proof.
i.
Fail leftest_rpapply.
on_leftest_function ltac:(fun x => idtac x).
Fail erewrite (f_equal3 Forall). (* dependent type *)
rpapply_raw H. Undo 1.
on_leftest_function_with_type ltac:(fun x => idtac x).
rpapply H.
Abort.
Goal (Forall2 (fun x y => x+y > 0)%nat [1 ; 2]%nat [3 ; 4]%nat) ->
(Forall2 (fun x y => x+y+1 > 1)%nat [1+0 ; 0+2]%nat [3 ; 4]%nat)
.
Proof.
i.
Fail leftest_rpapply.
on_leftest_function ltac:(fun x => idtac x).
Fail erewrite (f_equal3 Forall). (* dependent type *)
rpapply_raw H. Undo 1.
on_leftest_function_with_type ltac:(fun x => idtac x).
(* not "Forall2" or "Forall2 (A:=nat)". it is fed with types at maximum *)
rpapply H.
Abort.
(* Motivation: I want to distinguish excused ad-mits from normal ad-mits, *)
(* and further, I do not want to "grep" excused ones, so I give them different name. *)
(* Tactic Notation "AD-MIT" string(excuse) := idtac excuse; ad-mit. *)
(* above definition requires "Adm-itted" at the end of the proof, and I consider that not good *)
Definition ADMIT (excuse: String.string) {T: Type} : T. Admitted.
Tactic Notation "ADMIT" constr(excuse) := idtac excuse; exact (ADMIT excuse).
(* Clarify purpose of this file more clearly? *)
(* Should prevent circular dependency *)
Lemma mstore_aux_implies_vm_matches
Mem chunks gvs blck ofs Mem'
(MSTORE: mstore_aux Mem chunks gvs blck ofs = Some Mem')
:
<<VM_MATCHES: Forall2 vm_matches_typ gvs chunks>>
.
Proof.
red.
generalize dependent gvs.
revert_until chunks.
revert Mem.
induction chunks; ii; ss; des_ifs.
econs; [|eapply IHchunks; eauto].
unfold vm_matches_typ. ss.
des_bool; des.
apply memory_chunk_eq_prop in Heq. subst.
split; [ss|apply has_chunk_eq_prop; apply Heq1].
Qed.
Lemma mstore_implies_gv_chunks_match
TD Mem mpt t gvs algn Mem'
(MSTORE: mstore TD Mem mpt t gvs algn = Some Mem')
:
<<CHUNKS_MATCH: gv_chunks_match_typ TD gvs t>>
.
Proof.
red.
unfold mstore in *.
des_ifs.
unfold gv_chunks_match_typ.
rewrite Heq0.
eapply mstore_aux_implies_vm_matches; eauto.
Qed.
Lemma filter_map_spec
X Y
a b (f:X -> option Y) l
(IN: In a l)
(APP: f a = Some b)
: In b (filter_map f l).
Proof.
induction l; ss.
des.
- subst. rewrite APP.
econs. eauto.
- des_ifs; eauto.
constructor 2. eauto.
Qed.
Lemma filter_map_inv
X Y
b (f:X -> option Y) l
(IN: In b (filter_map f l))
: exists a, In a l /\ f a = Some b.
Proof.
revert IN.
induction l; ss.
des_ifs; i.
- ss. des.
+ subst. esplits; eauto.
+ apply IHl in IN. des.
esplits; eauto.
- apply IHl in IN. des.
esplits; eauto.
Qed.
Lemma list_prj2_inv
X Y (l:list (X * Y)) y
(IN: In y (list_prj2 X Y l))
: exists x, In (x, y) l.
Proof.
induction l; ss; i.
destruct a. ss. des.
- subst. esplits; eauto.
- apply IHl in IN. des.
esplits; eauto.
Qed.
Lemma wf_globals_eq maxb gl
:
<<EQ: genericvalues_inject.wf_globals maxb gl <-> memory_props.MemProps.wf_globals maxb gl>>
.
Proof.
split.
- econs; eauto. apply Pos.le_1_l.
- eapply memory_props.MemProps.redundant__wf_globals.
Qed.
Lemma int_add_0
(ofs : int32)
:
<<INT_ARITH: Int.signed 31 ofs =
Int.signed 31 (Int.add 31 ofs (Int.repr 31 0))>>
.
Proof.
unfold Int.add. ss.
replace (Int.repr 31 (Int.unsigned 31 ofs + 0)) with ofs; ss.
remember (Int.unsigned 31 ofs) as Z_ofs eqn:DEF_Z_ofs.
destruct ofs.
rewrite Z.add_0_r.
symmetry.
rewrite -> DEF_Z_ofs.
apply Int.repr_unsigned.
Qed.
(* TODO: can we do this with setoid? *)
Lemma InA_iff_In
A
(myeq: A -> A -> Prop)
(MYEQ_EQ: forall x y, myeq x y <-> x = y)
:
forall x xs, InA myeq x xs <-> In x xs.
Proof.
i.
split; i.
- ginduction xs; ii; ss.
{ inv H. }
inv H; ss.
+ apply MYEQ_EQ in H1. subst. left; ss.
+ right. eauto.
- ginduction xs; ii; ss.
des; ss.
+ subst. econs; eauto.
eapply MYEQ_EQ; eauto.
+ eapply InA_cons_tl; eauto.
Qed.