-
Notifications
You must be signed in to change notification settings - Fork 83
/
SepCancel.v
487 lines (427 loc) · 15 KB
/
SepCancel.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
(** Formal Reasoning About Programs <http://adam.chlipala.net/frap/>
* An entailment procedure for separation logic's assertion language
* Author: Adam Chlipala
* License: https://creativecommons.org/licenses/by-nc-nd/4.0/ *)
Require Import Frap Setoid Classes.Morphisms.
Set Implicit Arguments.
Module Type SEP.
Parameter hprop : Type.
Parameter lift : Prop -> hprop.
Parameter star : hprop -> hprop -> hprop.
Parameter exis : forall A, (A -> hprop) -> hprop.
Notation "[| P |]" := (lift P).
Infix "*" := star.
Notation "'exists' x .. y , p" := (exis (fun x => .. (exis (fun y => p)) ..)).
Parameters himp heq : hprop -> hprop -> Prop.
Infix "===" := heq (no associativity, at level 70).
Infix "===>" := himp (no associativity, at level 70).
Axiom himp_heq : forall p q, p === q
<-> (p ===> q /\ q ===> p).
Axiom himp_refl : forall p, p ===> p.
Axiom himp_trans : forall p q r, p ===> q -> q ===> r -> p ===> r.
Axiom lift_left : forall p (Q : Prop) r,
(Q -> p ===> r)
-> p * [| Q |] ===> r.
Axiom lift_right : forall p q (R : Prop),
p ===> q
-> R
-> p ===> q * [| R |].
Axiom extra_lift : forall (P : Prop) p,
P
-> p === [| P |] * p.
Axiom star_comm : forall p q, p * q === q * p.
Axiom star_assoc : forall p q r, p * (q * r) === (p * q) * r.
Axiom star_cancel : forall p1 p2 q1 q2, p1 ===> p2
-> q1 ===> q2
-> p1 * q1 ===> p2 * q2.
Axiom exis_gulp : forall A p (q : A -> _),
p * exis q === exis (fun x => p * q x).
Axiom exis_left : forall A (p : A -> _) q,
(forall x, p x ===> q)
-> exis p ===> q.
Axiom exis_right : forall A p (q : A -> _) x,
p ===> q x
-> p ===> exis q.
End SEP.
Module Make(Import S : SEP).
Add Parametric Relation : hprop himp
reflexivity proved by himp_refl
transitivity proved by himp_trans
as himp_rel.
Lemma heq_refl : forall p, p === p.
Proof.
intros; apply himp_heq; intuition (apply himp_refl).
Qed.
Lemma heq_sym : forall p q, p === q -> q === p.
Proof.
intros; apply himp_heq; apply himp_heq in H; intuition.
Qed.
Lemma heq_trans : forall p q r, p === q -> q === r -> p === r.
Proof.
intros; apply himp_heq; apply himp_heq in H; apply himp_heq in H0;
intuition (eauto using himp_trans).
Qed.
Add Parametric Relation : hprop heq
reflexivity proved by heq_refl
symmetry proved by heq_sym
transitivity proved by heq_trans
as heq_rel.
Global Instance himp_heq_mor : Proper (heq ==> heq ==> iff) himp.
Proof.
hnf; intros; hnf; intros.
apply himp_heq in H; apply himp_heq in H0.
intuition eauto using himp_trans.
Qed.
Add Parametric Morphism : star
with signature heq ==> heq ==> heq
as star_mor.
Proof.
intros; apply himp_heq; apply himp_heq in H; apply himp_heq in H0;
intuition (auto using star_cancel).
Qed.
Add Parametric Morphism : star
with signature himp ==> himp ==> himp
as star_mor'.
Proof.
auto using star_cancel.
Qed.
Global Instance exis_iff_morphism (A : Type) :
Proper (pointwise_relation A heq ==> heq) (@exis A).
Proof.
hnf; intros; apply himp_heq; intuition.
hnf in H.
apply exis_left; intro.
eapply exis_right.
assert (x x0 === y x0) by eauto.
apply himp_heq in H0; intuition eauto.
hnf in H.
apply exis_left; intro.
eapply exis_right.
assert (x x0 === y x0) by eauto.
apply himp_heq in H0; intuition eauto.
Qed.
Global Instance exis_imp_morphism (A : Type) :
Proper (pointwise_relation A himp ==> himp) (@exis A).
Proof.
hnf; intros.
apply exis_left; intro.
eapply exis_right.
unfold pointwise_relation in H.
eauto.
Qed.
Lemma star_combine_lift1 : forall P Q, [| P |] * [| Q |] ===> [| P /\ Q |].
Proof.
intros.
apply lift_left; intro.
rewrite extra_lift with (P := True); auto.
apply lift_left; intro.
rewrite extra_lift with (P := True) (p := [| P /\ Q |]); auto.
apply lift_right.
reflexivity.
tauto.
Qed.
Lemma star_combine_lift2 : forall P Q, [| P /\ Q |] ===> [| P |] * [| Q |].
Proof.
intros.
rewrite extra_lift with (P := True); auto.
apply lift_left; intro.
apply lift_right; try tauto.
rewrite extra_lift with (P := True) (p := [| P |]); auto.
apply lift_right; try tauto.
reflexivity.
Qed.
Lemma star_combine_lift : forall P Q, [| P |] * [| Q |] === [| P /\ Q |].
Proof.
intros.
apply himp_heq; auto using star_combine_lift1, star_combine_lift2.
Qed.
Lemma star_comm_lift : forall P q, [| P |] * q === q * [| P |].
Proof.
intros; apply star_comm.
Qed.
Lemma star_assoc_lift : forall p Q r,
(p * [| Q |]) * r === p * r * [| Q |].
Proof.
intros.
rewrite <- star_assoc.
rewrite (star_comm [| Q |]).
apply star_assoc.
Qed.
Lemma star_comm_exis : forall A (p : A -> _) q, exis p * q === q * exis p.
Proof.
intros; apply star_comm.
Qed.
Ltac lift :=
intros; apply himp_heq; split;
repeat (apply lift_left; intro);
repeat (apply lift_right; intuition).
Lemma lift_combine : forall p Q R,
p * [| Q |] * [| R |] === p * [| Q /\ R |].
Proof.
intros; apply himp_heq; split;
repeat (apply lift_left; intro);
repeat (apply lift_right; intuition).
Qed.
Lemma lift1_left : forall (P : Prop) q,
(P -> [| True |] ===> q)
-> [| P |] ===> q.
Proof.
intros.
rewrite (@extra_lift True [| P |]); auto.
apply lift_left; auto.
Qed.
Lemma lift1_right : forall p (Q : Prop),
Q
-> p ===> [| True |]
-> p ===> [| Q |].
Proof.
intros.
rewrite (@extra_lift True [| Q |]); auto.
apply lift_right; auto.
Qed.
Ltac normalize0 :=
match goal with
| [ |- context[star ?p (exis ?q)] ] => rewrite (exis_gulp p q)
| [ |- context[star (star ?p (lift ?q)) (lift ?r)] ] => rewrite (lift_combine p q r)
| [ |- context[star ?p (star ?q ?r)] ] => rewrite (star_assoc p q r)
| [ |- context[star (lift ?p) (lift ?q)] ] => rewrite (star_combine_lift p q)
| [ |- context[star (lift ?p) ?q ] ] => rewrite (star_comm_lift p q)
| [ |- context[star (star ?p (lift ?q)) ?r] ] => rewrite (star_assoc_lift p q r)
| [ |- context[star (exis ?p) ?q] ] => rewrite (star_comm_exis p q)
end.
Ltac normalizeL :=
(apply exis_left || apply lift_left; intro; try congruence)
|| match goal with
| [ |- lift ?P ===> _ ] =>
match P with
| True => fail 1
| _ => apply lift1_left; intro; try congruence
end
end.
Ltac normalizeR :=
match goal with
| [ |- _ ===> exis _ ] => eapply exis_right
| [ |- _ ===> _ * lift _ ] => apply lift_right
| [ |- _ ===> lift ?Q ] =>
match Q with
| True => fail 1
| _ => apply lift1_right
end
end.
Ltac normalize1 := normalize0 || normalizeL || normalizeR.
Lemma lift_uncombine : forall p P Q,
p * [| P /\ Q |] === p * [| P |] * [| Q |].
Proof.
lift.
Qed.
Ltac normalize2 :=
match goal with
| [ |- context[star ?p (lift (?P /\ ?Q))] ] => rewrite (lift_uncombine p P Q)
| [ |- context[star ?p (star ?q ?r)] ] => rewrite (star_assoc p q r)
end.
Ltac normalizeLeft :=
let s := fresh "s" in intro s;
let rhs := fresh "rhs" in
match goal with
| [ |- _ ===> ?Q ] => set (rhs := Q)
end;
simpl; intros; repeat (normalize0 || normalizeL);
repeat match goal with
| [ H : ex _ |- _ ===> _ ] => destruct H
| [ H : _ /\ _ |- _ ] => destruct H
| [ H : _ = _ |- _ ] => rewrite H
end; subst rhs.
Ltac normalize :=
simpl; intros; repeat normalize1; repeat normalize2;
repeat (match goal with
| [ H : ex _ |- _ ===> _ ] => destruct H
end; intuition idtac).
Ltac forAllAtoms p k :=
match p with
| ?q * ?r => forAllAtoms q k || forAllAtoms r k
| _ => k p
end.
Lemma stb1 : forall p q r,
(q * p) * r === q * r * p.
Proof.
intros; rewrite <- star_assoc; rewrite (star_comm p r); apply star_assoc.
Qed.
Ltac sendToBack part := repeat (rewrite (stb1 part) || rewrite (star_comm part)).
Theorem star_cancel' : forall p1 p2 q, p1 ===> p2
-> p1 * q ===> p2 * q.
Proof.
intros; apply star_cancel; auto using himp_refl.
Qed.
Theorem star_cancel'' : forall p q, lift True ===> q
-> p ===> p * q.
Proof.
intros.
eapply himp_trans.
rewrite extra_lift with (P := True); auto.
instantiate (1 := p * q).
rewrite star_comm.
apply star_cancel; auto.
reflexivity.
reflexivity.
Qed.
Module Type TRY_ME_FIRST.
Parameter try_me_first : hprop -> Prop.
Axiom try_me_first_easy : forall p, try_me_first p.
End TRY_ME_FIRST.
Module TMF : TRY_ME_FIRST.
Definition try_me_first (_ : hprop) := True.
Theorem try_me_first_easy : forall p, try_me_first p.
Proof.
constructor.
Qed.
End TMF.
Import TMF.
Export TMF.
Ltac cancel1 :=
match goal with
| [ |- ?p ===> ?q ] =>
(is_var q; fail 2)
|| forAllAtoms p ltac:(fun p0 =>
(let H := fresh in assert (H : try_me_first p0) by eauto; clear H);
sendToBack p0;
forAllAtoms q ltac:(fun q0 =>
(let H := fresh in assert (H : try_me_first q0) by eauto; clear H);
sendToBack q0;
apply star_cancel'))
end ||
match goal with
| [ |- _ ===> ?Q ] =>
match Q with
| _ => is_evar Q; fail 1
| ?Q _ => is_evar Q; fail 1
| _ => apply himp_refl
end
| [ |- ?p ===> ?q ] =>
(is_var q; fail 2)
|| forAllAtoms p ltac:(fun p0 =>
sendToBack p0;
forAllAtoms q ltac:(fun q0 =>
sendToBack q0;
apply star_cancel'))
| _ => progress autorewrite with core
end.
Ltac hide_evars :=
repeat match goal with
| [ |- ?P ===> _ ] => is_evar P; set P
| [ |- _ ===> ?Q ] => is_evar Q; set Q
| [ |- context[star ?P _] ] => is_evar P; set P
| [ |- context[star _ ?Q] ] => is_evar Q; set Q
| [ |- _ ===> exists v, _ * ?R v ] => is_evar R; set R
end.
Ltac restore_evars :=
repeat match goal with
| [ x := _ |- _ ] => subst x
end.
Fixpoint flattenAnds (Ps : list Prop) : Prop :=
match Ps with
| nil => True
| [P] => P
| P :: Ps => P /\ flattenAnds Ps
end.
Ltac allPuresFrom k :=
match goal with
| [ H : ?P |- _ ] =>
match type of P with
| Prop => generalize dependent H; allPuresFrom ltac:(fun Ps => k (P :: Ps))
end
| _ => intros; k (@nil Prop)
end.
Ltac whichToQuantify skip foundAlready k :=
match goal with
| [ x : ?T |- _ ] =>
match type of T with
| Prop => fail 1
| _ =>
match skip with
| context[x] => fail 1
| _ =>
match foundAlready with
| context[x] => fail 1
| _ => (instantiate (1 := lift (x = x)); fail 2)
|| (instantiate (1 := fun _ => lift (x = x)); fail 2)
|| (whichToQuantify skip (x, foundAlready) k)
end
end
end
| _ => k foundAlready
end.
Ltac quantifyOverThem vars e k :=
match vars with
| tt => k e
| (?x, ?vars') =>
match e with
| context[x] =>
match eval pattern x in e with
| ?f _ => quantifyOverThem vars' (exis f) k
end
| _ => quantifyOverThem vars' e k
end
end.
Ltac addQuantifiers P k :=
whichToQuantify tt tt ltac:(fun vars =>
quantifyOverThem vars P k).
Ltac addQuantifiersSkipping x P k :=
whichToQuantify x tt ltac:(fun vars =>
quantifyOverThem vars P k).
Ltac basic_cancel :=
normalize; repeat cancel1; repeat match goal with
| [ H : _ /\ _ |- _ ] => destruct H
| [ |- _ /\ _ ] => split
end; eassumption || apply I.
Ltac beautify := repeat match goal with
| [ H : True |- _ ] => clear H
| [ H : ?P, H' : ?P |- _ ] =>
match type of P with
| Prop => clear H'
end
| [ H : _ /\ _ |- _ ] => destruct H
end.
Ltac cancel := hide_evars; normalize; repeat cancel1; restore_evars; beautify;
try match goal with
| [ |- _ ===> ?p * ?q ] =>
((is_evar p; fail 1) || apply star_cancel'')
|| ((is_evar q; fail 1) || (rewrite (star_comm p q); apply star_cancel''))
end;
try match goal with
| [ |- ?P ===> _ ] => sendToBack P;
match goal with
| [ |- ?P ===> ?Q * ?P ] => is_evar Q;
rewrite (star_comm Q P);
allPuresFrom ltac:(fun Ps =>
match Ps with
| nil => instantiate (1 := lift True)
| _ =>
let Ps' := eval simpl in (flattenAnds Ps) in
addQuantifiers (lift Ps') ltac:(fun e => instantiate (1 := e))
end;
basic_cancel)
end
| [ |- ?P ===> ?Q ] => is_evar Q;
allPuresFrom ltac:(fun Ps =>
match Ps with
| nil => reflexivity
| _ =>
let Ps' := eval simpl in (flattenAnds Ps) in
addQuantifiers (star P (lift Ps')) ltac:(fun e => instantiate (1 := e));
basic_cancel
end)
| [ |- ?P ===> ?Q ?x ] => is_evar Q;
allPuresFrom ltac:(fun Ps =>
match Ps with
| nil => reflexivity
| _ =>
let Ps' := eval simpl in (flattenAnds Ps) in
addQuantifiersSkipping x (star P (lift Ps'))
ltac:(fun e => match eval pattern x in e with
| ?f _ => instantiate (1 := f)
end);
basic_cancel
end)
| [ |- _ ===> _ ] => intuition (try congruence)
end; intuition idtac; beautify.
End Make.