File: monpred.v

package info (click to toggle)
coq-iris 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,116 kB
  • sloc: python: 130; makefile: 61; sh: 28; sed: 2
file content (635 lines) | stat: -rw-r--r-- 31,429 bytes parent folder | download
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
From iris.bi Require Export monpred.
From iris.bi Require Import plainly.
From iris.proofmode Require Import proofmode classes_make modality_instances.
From iris.prelude Require Import options.

Class MakeMonPredAt {I : biIndex} {PROP : bi} (i : I)
      (P : monPred I PROP) (π“Ÿ : PROP) :=
  make_monPred_at : P i ⊣⊒ π“Ÿ.
Global Arguments MakeMonPredAt {_ _} _ _%_I _%_I.
(** Since [MakeMonPredAt] is used by [AsEmpValid] to import lemmas into the
proof mode, the index [I] and BI [PROP] often contain evars. Hence, it is
important to use the mode [!] also for the first two arguments. *)
Global Hint Mode MakeMonPredAt ! ! - ! - : typeclass_instances.

Class IsBiIndexRel {I : biIndex} (i j : I) := is_bi_index_rel : i βŠ‘ j.
Global Hint Mode IsBiIndexRel + - - : typeclass_instances.
Global Instance is_bi_index_rel_refl {I : biIndex} (i : I) : IsBiIndexRel i i | 0.
Proof. by rewrite /IsBiIndexRel. Qed.
Global Hint Extern 1 (IsBiIndexRel _ _) => unfold IsBiIndexRel; assumption
            : typeclass_instances.

(** Frame [𝓑] into the goal [monPred_at P i] and determine the remainder [𝓠].
    Used when framing encounters a monPred_at in the goal. *)
Class FrameMonPredAt {I : biIndex} {PROP : bi} (p : bool) (i : I)
      (𝓑 : PROP) (P : monPred I PROP) (𝓠 : PROP) :=
  frame_monPred_at : β–‘?p 𝓑 βˆ— 𝓠 ⊒ P i.
Global Arguments FrameMonPredAt {_ _} _ _ _%_I _%_I _%_I.
Global Hint Mode FrameMonPredAt + + + - ! ! - : typeclass_instances.

Section modalities.
  Context {I : biIndex} {PROP : bi}.

  Lemma modality_objectively_mixin :
    modality_mixin (@monPred_objectively I PROP)
      (MIEnvFilter Objective) (MIEnvFilter Objective).
  Proof.
    split; simpl; split_and?; intros;
      try select (TCDiag _ _ _) (fun H => destruct H);
      eauto using bi.equiv_entails_1_2, objective_objectively,
        monPred_objectively_mono, monPred_objectively_and,
        monPred_objectively_sep_2 with typeclass_instances.
  Qed.
  Definition modality_objectively :=
    Modality _ modality_objectively_mixin.
End modalities.

Section bi.
Context {I : biIndex} {PROP : bi}.
Local Notation monPredI := (monPredI I PROP).
Local Notation monPred := (monPred I PROP).
Local Notation MakeMonPredAt := (@MakeMonPredAt I PROP).
Implicit Types P Q R : monPred.
Implicit Types π“Ÿ 𝓠 𝓑 : PROP.
Implicit Types Ο† : Prop.
Implicit Types i j : I.

Global Instance from_modal_objectively P :
  FromModal True modality_objectively (<obj> P) (<obj> P) P | 1.
Proof. by rewrite /FromModal. Qed.
Global Instance from_modal_subjectively P :
  FromModal True modality_id (<subj> P) (<subj> P) P | 1.
Proof. by rewrite /FromModal /= -monPred_subjectively_intro. Qed.

Global Instance from_modal_affinely_monPred_at Ο† `(sel : A) P Q 𝓠 i :
  FromModal Ο† modality_affinely sel P Q β†’
  MakeMonPredAt i Q 𝓠 β†’
  FromModal Ο† modality_affinely sel (P i) 𝓠 | 0.
Proof.
  rewrite /FromModal /MakeMonPredAt /==> HPQ <- ?.
  by rewrite -HPQ // monPred_at_affinely.
Qed.
Global Instance from_modal_persistently_monPred_at Ο† `(sel : A) P Q 𝓠 i :
  FromModal Ο† modality_persistently sel P Q β†’
  MakeMonPredAt i Q 𝓠 β†’
  FromModal Ο† modality_persistently sel (P i) 𝓠 | 0.
Proof.
  rewrite /FromModal /MakeMonPredAt /==> HPQ <- ?.
  by rewrite -HPQ // monPred_at_persistently.
Qed.
Global Instance from_modal_intuitionistically_monPred_at Ο† `(sel : A) P Q 𝓠 i :
  FromModal Ο† modality_intuitionistically sel P Q β†’
  MakeMonPredAt i Q 𝓠 β†’
  FromModal Ο† modality_intuitionistically sel (P i) 𝓠 | 0.
Proof.
  rewrite /FromModal /MakeMonPredAt /==> HPQ <- ?.
  by rewrite -HPQ // monPred_at_affinely monPred_at_persistently.
Qed.
Global Instance from_modal_id_monPred_at Ο† `(sel : A) P Q 𝓠 i :
  FromModal Ο† modality_id sel P Q β†’ MakeMonPredAt i Q 𝓠 β†’
  FromModal Ο† modality_id sel (P i) 𝓠.
Proof. rewrite /FromModal /MakeMonPredAt=> HPQ <- ?. by rewrite -HPQ. Qed.

Global Instance make_monPred_at_pure Ο† i : MakeMonPredAt i βŒœΟ†βŒ βŒœΟ†βŒ.
Proof. by rewrite /MakeMonPredAt monPred_at_pure. Qed.
Global Instance make_monPred_at_emp i : MakeMonPredAt i emp emp.
Proof. by rewrite /MakeMonPredAt monPred_at_emp. Qed.
Global Instance make_monPred_at_sep i P π“Ÿ Q 𝓠 :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i Q 𝓠 β†’
  MakeMonPredAt i (P βˆ— Q) (π“Ÿ βˆ— 𝓠).
Proof. by rewrite /MakeMonPredAt monPred_at_sep=><-<-. Qed.
Global Instance make_monPred_at_and i P π“Ÿ Q 𝓠 :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i Q 𝓠 β†’
  MakeMonPredAt i (P ∧ Q) (π“Ÿ ∧ 𝓠).
Proof. by rewrite /MakeMonPredAt monPred_at_and=><-<-. Qed.
Global Instance make_monPred_at_or i P π“Ÿ Q 𝓠 :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i Q 𝓠 β†’
  MakeMonPredAt i (P ∨ Q) (π“Ÿ ∨ 𝓠).
Proof. by rewrite /MakeMonPredAt monPred_at_or=><-<-. Qed.
Global Instance make_monPred_at_forall {A} i (Ξ¦ : A β†’ monPred) (Ξ¨ : A β†’ PROP) :
  (βˆ€ a, MakeMonPredAt i (Ξ¦ a) (Ξ¨ a)) β†’ MakeMonPredAt i (βˆ€ a, Ξ¦ a) (βˆ€ a, Ξ¨ a).
Proof. rewrite /MakeMonPredAt monPred_at_forall=>H. by setoid_rewrite <- H. Qed.
Global Instance make_monPred_at_exists {A} i (Ξ¦ : A β†’ monPred) (Ξ¨ : A β†’ PROP) :
  (βˆ€ a, MakeMonPredAt i (Ξ¦ a) (Ξ¨ a)) β†’ MakeMonPredAt i (βˆƒ a, Ξ¦ a) (βˆƒ a, Ξ¨ a).
Proof. rewrite /MakeMonPredAt monPred_at_exist=>H. by setoid_rewrite <- H. Qed.
Global Instance make_monPred_at_persistently i P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i (<pers> P) (<pers> π“Ÿ).
Proof. by rewrite /MakeMonPredAt monPred_at_persistently=><-. Qed.
Global Instance make_monPred_at_affinely i P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i (<affine> P) (<affine> π“Ÿ).
Proof. by rewrite /MakeMonPredAt monPred_at_affinely=><-. Qed.
Global Instance make_monPred_at_intuitionistically i P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i (β–‘ P) (β–‘ π“Ÿ).
Proof. by rewrite /MakeMonPredAt monPred_at_intuitionistically=><-. Qed.
Global Instance make_monPred_at_absorbingly i P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i (<absorb> P) (<absorb> π“Ÿ).
Proof. by rewrite /MakeMonPredAt monPred_at_absorbingly=><-. Qed.
Global Instance make_monPred_at_persistently_if i P π“Ÿ p :
  MakeMonPredAt i P π“Ÿ β†’
  MakeMonPredAt i (<pers>?p P) (<pers>?p π“Ÿ).
Proof. destruct p; simpl; apply _. Qed.
Global Instance make_monPred_at_affinely_if i P π“Ÿ p :
  MakeMonPredAt i P π“Ÿ β†’
  MakeMonPredAt i (<affine>?p P) (<affine>?p π“Ÿ).
Proof. destruct p; simpl; apply _. Qed.
Global Instance make_monPred_at_absorbingly_if i P π“Ÿ p :
  MakeMonPredAt i P π“Ÿ β†’
  MakeMonPredAt i (<absorb>?p P) (<absorb>?p π“Ÿ).
Proof. destruct p; simpl; apply _. Qed.
Global Instance make_monPred_at_intuitionistically_if i P π“Ÿ p :
  MakeMonPredAt i P π“Ÿ β†’
  MakeMonPredAt i (β–‘?p P) (β–‘?p π“Ÿ).
Proof. destruct p; simpl; apply _. Qed.
Global Instance make_monPred_at_embed i π“Ÿ : MakeMonPredAt i βŽ‘π“ŸβŽ€ π“Ÿ.
Proof. by rewrite /MakeMonPredAt monPred_at_embed. Qed.
Global Instance make_monPred_at_in i j : MakeMonPredAt j (monPred_in i) ⌜i βŠ‘ j⌝.
Proof. by rewrite /MakeMonPredAt monPred_at_in. Qed.
Global Instance make_monPred_at_default i P : MakeMonPredAt i P (P i) | 100.
Proof. by rewrite /MakeMonPredAt. Qed.
Global Instance make_monPred_at_bupd `{!BiBUpd PROP} i P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i (|==> P) (|==> π“Ÿ).
Proof. by rewrite /MakeMonPredAt monPred_at_bupd=> <-. Qed.

Global Instance from_assumption_make_monPred_at_l p i j P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ IsBiIndexRel j i β†’ KnownLFromAssumption p (P j) π“Ÿ.
Proof.
  rewrite /MakeMonPredAt /KnownLFromAssumption /FromAssumption /IsBiIndexRel=><- ->.
  apply  bi.intuitionistically_if_elim.
Qed.
Global Instance from_assumption_make_monPred_at_r p i j P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ IsBiIndexRel i j β†’ KnownRFromAssumption p π“Ÿ (P j).
Proof.
  rewrite /MakeMonPredAt /KnownRFromAssumption /FromAssumption /IsBiIndexRel=><- ->.
  apply  bi.intuitionistically_if_elim.
Qed.

Global Instance from_assumption_make_monPred_objectively p P Q :
  FromAssumption p P Q β†’ KnownLFromAssumption p (<obj> P) Q.
Proof.
  by rewrite /KnownLFromAssumption /FromAssumption monPred_objectively_elim.
Qed.
Global Instance from_assumption_make_monPred_subjectively p P Q :
  FromAssumption p P Q β†’ KnownRFromAssumption p P (<subj> Q).
Proof.
  by rewrite /KnownRFromAssumption /FromAssumption -monPred_subjectively_intro.
Qed.

Global Instance as_emp_valid_monPred_at Ο† P (Ξ¦ : I β†’ PROP) :
  AsEmpValid0 Ο† P β†’ (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ AsEmpValid Ο† (βˆ€ i, Ξ¦ i) | 100.
Proof.
  rewrite /MakeMonPredAt /AsEmpValid0 /AsEmpValid /bi_emp_valid=> -> EQ.
  setoid_rewrite <-EQ. split.
  - move=>[H]. apply bi.forall_intro=>i. rewrite -H. by rewrite monPred_at_emp.
  - move=>HP. split=>i. rewrite monPred_at_emp HP bi.forall_elim //.
Qed.
Global Instance as_emp_valid_monPred_at_wand Ο† P Q (Ξ¦ Ξ¨ : I β†’ PROP) :
  AsEmpValid0 Ο† (P -βˆ— Q) β†’
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ (βˆ€ i, MakeMonPredAt i Q (Ξ¨ i)) β†’
  AsEmpValid Ο† (βˆ€ i, Ξ¦ i -βˆ— Ξ¨ i).
Proof.
  rewrite /AsEmpValid0 /AsEmpValid /MakeMonPredAt. intros -> EQ1 EQ2.
  setoid_rewrite <-EQ1. setoid_rewrite <-EQ2. split.
  - move=>/bi.wand_entails HP. setoid_rewrite HP. by iIntros (i) "$".
  - move=>HP. apply bi.entails_wand. split=>i. iIntros "H". by iApply HP.
Qed.
Global Instance as_emp_valid_monPred_at_equiv Ο† P Q (Ξ¦ Ξ¨ : I β†’ PROP) :
  AsEmpValid0 Ο† (P βˆ—-βˆ— Q) β†’
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ (βˆ€ i, MakeMonPredAt i Q (Ξ¨ i)) β†’
  AsEmpValid Ο† (βˆ€ i, Ξ¦ i βˆ—-βˆ— Ξ¨ i).
Proof.
  rewrite /AsEmpValid0 /AsEmpValid /MakeMonPredAt. intros -> EQ1 EQ2.
  setoid_rewrite <-EQ1. setoid_rewrite <-EQ2. split.
  - move=>/bi.wand_iff_equiv HP. setoid_rewrite HP. iIntros. iSplit; iIntros "$".
  - move=>HP. apply bi.equiv_wand_iff. split=>i. by iSplit; iIntros; iApply HP.
Qed.

Global Instance into_pure_monPred_at P Ο† i : IntoPure P Ο† β†’ IntoPure (P i) Ο†.
Proof. rewrite /IntoPure=>->. by rewrite monPred_at_pure. Qed.
Global Instance from_pure_monPred_at a P Ο† i : FromPure a P Ο† β†’ FromPure a (P i) Ο†.
Proof. rewrite /FromPure=><-. by rewrite monPred_at_affinely_if monPred_at_pure. Qed.
Global Instance into_pure_monPred_in i j : @IntoPure PROP (monPred_in i j) (i βŠ‘ j).
Proof. by rewrite /IntoPure monPred_at_in. Qed.
Global Instance from_pure_monPred_in i j : @FromPure PROP false (monPred_in i j) (i βŠ‘ j).
Proof. by rewrite /FromPure monPred_at_in. Qed.

Global Instance into_persistent_monPred_at p P Q 𝓠 i :
  IntoPersistent p P Q β†’ MakeMonPredAt i Q 𝓠 β†’ IntoPersistent p (P i) 𝓠 | 0.
Proof.
  rewrite /IntoPersistent /MakeMonPredAt  =>-[/(_ i) ?] <-.
  by rewrite -monPred_at_persistently -monPred_at_persistently_if.
Qed.

Lemma into_wand_monPred_at_unknown_unknown p q R P π“Ÿ Q 𝓠 i :
  IntoWand p q R P Q β†’ MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i Q 𝓠 β†’
  IntoWand p q (R i) π“Ÿ 𝓠.
Proof.
  rewrite /IntoWand /MakeMonPredAt /bi_affinely_if /bi_persistently_if.
  destruct p, q=> /bi.wand_elim_l' [/(_ i) H] <- <-; apply bi.wand_intro_r;
  revert H; by rewrite monPred_at_sep ?monPred_at_affinely ?monPred_at_persistently.
Qed.
Lemma into_wand_monPred_at_unknown_known p q R P π“Ÿ Q i j :
  IsBiIndexRel i j β†’ IntoWand p q R P Q β†’
  MakeMonPredAt j P π“Ÿ β†’ IntoWand p q (R i) π“Ÿ (Q j).
Proof.
  rewrite /IntoWand /IsBiIndexRel /MakeMonPredAt=>-> ? ?.
  eapply into_wand_monPred_at_unknown_unknown=>//. apply _.
Qed.
Lemma into_wand_monPred_at_known_unknown_le p q R P Q 𝓠 i j :
  IsBiIndexRel i j β†’ IntoWand p q R P Q β†’
  MakeMonPredAt j Q 𝓠 β†’ IntoWand p q (R i) (P j) 𝓠.
Proof.
  rewrite /IntoWand /IsBiIndexRel /MakeMonPredAt=>-> ? ?.
  eapply into_wand_monPred_at_unknown_unknown=>//. apply _.
Qed.
Lemma into_wand_monPred_at_known_unknown_ge p q R P Q 𝓠 i j :
  IsBiIndexRel i j β†’ IntoWand p q R P Q β†’
  MakeMonPredAt j Q 𝓠 β†’ IntoWand p q (R j) (P i) 𝓠.
Proof.
  rewrite /IntoWand /IsBiIndexRel /MakeMonPredAt=>-> ? ?.
  eapply into_wand_monPred_at_unknown_unknown=>//. apply _.
Qed.

Global Instance into_wand_wand'_monPred p q P Q π“Ÿ 𝓠 i :
  IntoWand' p q ((P -βˆ— Q) i) π“Ÿ 𝓠 β†’ IntoWand p q ((P -βˆ— Q) i) π“Ÿ 𝓠 | 100.
Proof. done. Qed.
Global Instance into_wand_impl'_monPred p q P Q π“Ÿ 𝓠 i :
  IntoWand' p q ((P β†’ Q) i) π“Ÿ 𝓠 β†’ IntoWand p q ((P β†’ Q) i) π“Ÿ 𝓠 | 100.
Proof. done. Qed.

Global Instance from_forall_monPred_at_wand P Q (Ξ¦ Ξ¨ : I β†’ PROP) i :
  (βˆ€ j, MakeMonPredAt j P (Ξ¦ j)) β†’ (βˆ€ j, MakeMonPredAt j Q (Ξ¨ j)) β†’
  FromForall ((P -βˆ— Q) i)%I (Ξ» j, ⌜i βŠ‘ j⌝ β†’ Ξ¦ j -βˆ— Ξ¨ j)%I (to_ident_name idx).
Proof.
  rewrite /FromForall /MakeMonPredAt monPred_at_wand=> H1 H2. do 2 f_equiv.
  by rewrite H1 H2.
Qed.
Global Instance from_forall_monPred_at_impl P Q (Ξ¦ Ξ¨ : I β†’ PROP) i :
  (βˆ€ j, MakeMonPredAt j P (Ξ¦ j)) β†’ (βˆ€ j, MakeMonPredAt j Q (Ξ¨ j)) β†’
  FromForall ((P β†’ Q) i)%I (Ξ» j, ⌜i βŠ‘ j⌝ β†’ Ξ¦ j β†’ Ξ¨ j)%I (to_ident_name idx).
Proof.
  rewrite /FromForall /MakeMonPredAt monPred_at_impl=> H1 H2. do 2 f_equiv.
  by rewrite H1 H2 bi.pure_impl_forall.
Qed.

Global Instance into_forall_monPred_at_index P i :
  IntoForall (P i) (Ξ» j, ⌜i βŠ‘ j⌝ β†’ P j)%I | 100.
Proof.
  rewrite /IntoForall. setoid_rewrite bi.pure_impl_forall.
  do 2 apply bi.forall_intro=>?. by f_equiv.
Qed.

Global Instance from_and_monPred_at P Q1 𝓠1 Q2 𝓠2 i :
  FromAnd P Q1 Q2 β†’ MakeMonPredAt i Q1 𝓠1 β†’ MakeMonPredAt i Q2 𝓠2 β†’
  FromAnd (P i) 𝓠1 𝓠2.
Proof.
  rewrite /FromAnd /MakeMonPredAt /MakeMonPredAt=> <- <- <-.
  by rewrite monPred_at_and.
Qed.
Global Instance into_and_monPred_at p P Q1 𝓠1 Q2 𝓠2 i :
  IntoAnd p P Q1 Q2 β†’ MakeMonPredAt i Q1 𝓠1 β†’ MakeMonPredAt i Q2 𝓠2 β†’
  IntoAnd p (P i) 𝓠1 𝓠2.
Proof.
  rewrite /IntoAnd /MakeMonPredAt /bi_affinely_if /bi_persistently_if.
  destruct p=>-[/(_ i) H] <- <-; revert H;
  by rewrite ?monPred_at_affinely ?monPred_at_persistently monPred_at_and.
Qed.

Global Instance from_sep_monPred_at P Q1 𝓠1 Q2 𝓠2 i :
  FromSep P Q1 Q2 β†’ MakeMonPredAt i Q1 𝓠1 β†’ MakeMonPredAt i Q2 𝓠2 β†’
  FromSep (P i) 𝓠1 𝓠2.
Proof. rewrite /FromSep /MakeMonPredAt=> <- <- <-. by rewrite monPred_at_sep. Qed.
Global Instance into_sep_monPred_at P Q1 𝓠1 Q2 𝓠2 i :
  IntoSep P Q1 Q2 β†’ MakeMonPredAt i Q1 𝓠1 β†’ MakeMonPredAt i Q2 𝓠2 β†’
  IntoSep (P i) 𝓠1 𝓠2.
Proof. rewrite /IntoSep /MakeMonPredAt=> -> <- <-. by rewrite monPred_at_sep. Qed.
Global Instance from_or_monPred_at P Q1 𝓠1 Q2 𝓠2 i :
  FromOr P Q1 Q2 β†’ MakeMonPredAt i Q1 𝓠1 β†’ MakeMonPredAt i Q2 𝓠2 β†’
  FromOr (P i) 𝓠1 𝓠2.
Proof. rewrite /FromOr /MakeMonPredAt=> <- <- <-. by rewrite monPred_at_or. Qed.
Global Instance into_or_monPred_at P Q1 𝓠1 Q2 𝓠2 i :
  IntoOr P Q1 Q2 β†’ MakeMonPredAt i Q1 𝓠1 β†’ MakeMonPredAt i Q2 𝓠2 β†’
  IntoOr (P i) 𝓠1 𝓠2.
Proof. rewrite /IntoOr /MakeMonPredAt=> -> <- <-. by rewrite monPred_at_or. Qed.

Global Instance from_exist_monPred_at {A} P (Ξ¦ : A β†’ monPred) (Ξ¨ : A β†’ PROP) i :
  FromExist P Ξ¦ β†’ (βˆ€ a, MakeMonPredAt i (Ξ¦ a) (Ξ¨ a)) β†’ FromExist (P i) Ξ¨.
Proof.
  rewrite /FromExist /MakeMonPredAt=><- H. setoid_rewrite <- H.
  by rewrite monPred_at_exist.
Qed.
Global Instance into_exist_monPred_at {A} P (Ξ¦ : A β†’ monPred) name (Ξ¨ : A β†’ PROP) i :
  IntoExist P Ξ¦ name β†’ (βˆ€ a, MakeMonPredAt i (Ξ¦ a) (Ξ¨ a)) β†’ IntoExist (P i) Ξ¨ name.
Proof.
  rewrite /IntoExist /MakeMonPredAt=>-> H. setoid_rewrite <- H.
  by rewrite monPred_at_exist.
Qed.

Global Instance from_forall_monPred_at_objectively P (Ξ¦ : I β†’ PROP) i :
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ FromForall ((<obj> P) i)%I Ξ¦ (to_ident_name idx).
Proof.
  rewrite /FromForall /MakeMonPredAt monPred_at_objectively=>H. by setoid_rewrite <- H.
Qed.
Global Instance into_forall_monPred_at_objectively P (Ξ¦ : I β†’ PROP) i :
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ IntoForall ((<obj> P) i) Ξ¦.
Proof.
  rewrite /IntoForall /MakeMonPredAt monPred_at_objectively=>H. by setoid_rewrite <- H.
Qed.

Global Instance from_exist_monPred_at_ex P (Ξ¦ : I β†’ PROP) i :
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ FromExist ((<subj> P) i) Ξ¦.
Proof.
  rewrite /FromExist /MakeMonPredAt monPred_at_subjectively=>H. by setoid_rewrite <- H.
Qed.
(* TODO: this implementation uses [idx] as the automatic name for the index. In
theory a monPred could define an appropriate metavariable for indices with an
[ident_name] argument to [MakeMonPredAt], but this is not implemented. *)
Global Instance into_exist_monPred_at_ex P (Ξ¦ : I β†’ PROP) i :
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’ IntoExist ((<subj> P) i) Ξ¦ (to_ident_name idx).
Proof.
  rewrite /IntoExist /MakeMonPredAt monPred_at_subjectively=>H. by setoid_rewrite <- H.
Qed.

Global Instance from_forall_monPred_at {A} P (Ξ¦ : A β†’ monPred) name (Ξ¨ : A β†’ PROP) i :
  FromForall P Ξ¦ name β†’ (βˆ€ a, MakeMonPredAt i (Ξ¦ a) (Ξ¨ a)) β†’ FromForall (P i) Ξ¨ name.
Proof.
  rewrite /FromForall /MakeMonPredAt=><- H. setoid_rewrite <- H.
  by rewrite monPred_at_forall.
Qed.
Global Instance into_forall_monPred_at {A} P (Ξ¦ : A β†’ monPred) (Ξ¨ : A β†’ PROP) i :
  IntoForall P Ξ¦ β†’ (βˆ€ a, MakeMonPredAt i (Ξ¦ a) (Ξ¨ a)) β†’ IntoForall (P i) Ξ¨.
Proof.
  rewrite /IntoForall /MakeMonPredAt=>-> H. setoid_rewrite <- H.
  by rewrite monPred_at_forall.
Qed.

(* Framing. *)
Global Instance frame_monPred_at_enter p i 𝓑 P 𝓠 :
  FrameMonPredAt p i 𝓑 P 𝓠 β†’ Frame p 𝓑 (P i) 𝓠 | 2.
Proof. intros. done. Qed.
Global Instance frame_monPred_at_here p P i j :
  IsBiIndexRel i j β†’ FrameMonPredAt p j (P i) P emp | 0.
Proof.
  rewrite /FrameMonPredAt /IsBiIndexRel right_id bi.intuitionistically_if_elim=> -> //.
Qed.

Global Instance frame_monPred_at_embed p 𝓑 𝓠 π“Ÿ i :
  Frame p 𝓑 π“Ÿ 𝓠 β†’ FrameMonPredAt p i 𝓑 (embed (B:=monPredI) π“Ÿ) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_embed. Qed.
Global Instance frame_monPred_at_sep p P Q 𝓑 𝓠 i :
  Frame p 𝓑 (P i βˆ— Q i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (P βˆ— Q) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_sep. Qed.
Global Instance frame_monPred_at_and p P Q 𝓑 𝓠 i :
  Frame p 𝓑 (P i ∧ Q i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (P ∧ Q) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_and. Qed.
Global Instance frame_monPred_at_or p P Q 𝓑 𝓠 i :
  Frame p 𝓑 (P i ∨ Q i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (P ∨ Q) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_or. Qed.
Global Instance frame_monPred_at_wand p P R Q1 Q2 i j :
  IsBiIndexRel i j β†’
  Frame p R Q1 Q2 β†’
  FrameMonPredAt p j (R i) (P -βˆ— Q1) ((P -βˆ— Q2) i).
Proof.
  rewrite /IsBiIndexRel /Frame /FrameMonPredAt=>-> Hframe.
  rewrite -monPred_at_intuitionistically_if -monPred_at_sep. apply monPred_in_entails.
  change ((β–‘?p R βˆ— (P -βˆ— Q2)) ⊒ P -βˆ— Q1). apply bi.wand_intro_r.
  rewrite -assoc bi.wand_elim_l. done.
Qed.
Global Instance frame_monPred_at_impl P R Q1 Q2 i j :
  IsBiIndexRel i j β†’
  Frame true R Q1 Q2 β†’
  FrameMonPredAt true j (R i) (P β†’ Q1) ((P β†’ Q2) i).
Proof.
  rewrite /IsBiIndexRel /Frame /FrameMonPredAt=>-> Hframe.
  rewrite -monPred_at_intuitionistically_if -monPred_at_sep. apply monPred_in_entails.
  change ((β–‘ R βˆ— (P β†’ Q2)) ⊒ P β†’ Q1).
  rewrite -bi.persistently_and_intuitionistically_sep_l. apply bi.impl_intro_r.
  rewrite -assoc bi.impl_elim_l bi.persistently_and_intuitionistically_sep_l. done.
Qed.
Global Instance frame_monPred_at_forall {X : Type} p (Ξ¨ : X β†’ monPred) 𝓑 𝓠 i :
  Frame p 𝓑 (βˆ€ x, Ξ¨ x i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (βˆ€ x, Ξ¨ x) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_forall. Qed.
Global Instance frame_monPred_at_exist {X : Type} p (Ξ¨ : X β†’ monPred) 𝓑 𝓠 i :
  Frame p 𝓑 (βˆƒ x, Ξ¨ x i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (βˆƒ x, Ξ¨ x) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_exist. Qed.

Global Instance frame_monPred_at_absorbingly p P 𝓑 𝓠 i :
  Frame p 𝓑 (<absorb> P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (<absorb> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_absorbingly. Qed.
Global Instance frame_monPred_at_affinely p P 𝓑 𝓠 i :
  Frame p 𝓑 (<affine> P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (<affine> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_affinely. Qed.
Global Instance frame_monPred_at_persistently p P 𝓑 𝓠 i :
  Frame p 𝓑 (<pers> P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (<pers> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_persistently. Qed.
Global Instance frame_monPred_at_intuitionistically p P 𝓑 𝓠 i :
  Frame p 𝓑 (β–‘ P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (β–‘ P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_intuitionistically. Qed.
Global Instance frame_monPred_at_objectively p P 𝓑 𝓠 i :
  Frame p 𝓑 (βˆ€ i, P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (<obj> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_objectively. Qed.
Global Instance frame_monPred_at_subjectively p P 𝓑 𝓠 i :
  Frame p 𝓑 (βˆƒ i, P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (<subj> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_subjectively. Qed.
Global Instance frame_monPred_at_bupd `{!BiBUpd PROP} p P 𝓑 𝓠 i :
  Frame p 𝓑 (|==> P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (|==> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_bupd. Qed.

Global Instance into_embed_objective P :
  Objective P β†’ IntoEmbed P (βˆ€ i, P i).
Proof.
  rewrite /IntoEmbed=> ?.
  by rewrite {1}(objective_objectively P) monPred_objectively_unfold.
Qed.

Global Instance elim_modal_at_bupd_goal `{!BiBUpd PROP} Ο† p p' π“Ÿ π“Ÿ' Q Q' i :
  ElimModal Ο† p p' π“Ÿ π“Ÿ' (|==> Q i) (|==> Q' i) β†’
  ElimModal Ο† p p' π“Ÿ π“Ÿ' ((|==> Q) i) ((|==> Q') i).
Proof. by rewrite /ElimModal !monPred_at_bupd. Qed.
Global Instance elim_modal_at_bupd_hyp `{!BiBUpd PROP} Ο† p p' P π“Ÿ π“Ÿ' 𝓠 𝓠' i:
  MakeMonPredAt i P π“Ÿ β†’
  ElimModal Ο† p p' (|==> π“Ÿ) π“Ÿ' 𝓠 𝓠' β†’
  ElimModal Ο† p p' ((|==> P) i) π“Ÿ' 𝓠 𝓠'.
Proof. by rewrite /MakeMonPredAt /ElimModal monPred_at_bupd=><-. Qed.
Global Instance elim_modal_at Ο† p p' π“Ÿ π“Ÿ' P P' V:
  ElimModal Ο† p p' βŽ‘π“ŸβŽ€ βŽ‘π“Ÿ'⎀ P P' β†’ ElimModal Ο† p p' π“Ÿ π“Ÿ' (P V) (P' V).
Proof.
  rewrite /ElimModal -!embed_intuitionistically_if.
  iIntros (HH Hφ) "[? HP]". iApply HH; [done|]. iFrame. iIntros (? <-) "?".
  by iApply "HP".
Qed.

Global Instance add_modal_at_bupd_goal `{!BiBUpd PROP} Ο† π“Ÿ π“Ÿ' Q i :
  AddModal π“Ÿ π“Ÿ' (|==> Q i)%I β†’ AddModal π“Ÿ π“Ÿ' ((|==> Q) i).
Proof. by rewrite /AddModal !monPred_at_bupd. Qed.

Global Instance from_forall_monPred_at_plainly `{!BiPlainly PROP} i P Ξ¦ :
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’
  FromForall ((β–  P) i) (Ξ» j, β–  (Ξ¦ j))%I (to_ident_name idx).
Proof.
  rewrite /FromForall /MakeMonPredAt=>HPΦ. rewrite monPred_at_plainly.
  by setoid_rewrite HPΦ.
Qed.
Global Instance into_forall_monPred_at_plainly `{!BiPlainly PROP} i P Ξ¦ :
  (βˆ€ i, MakeMonPredAt i P (Ξ¦ i)) β†’
  IntoForall ((β–  P) i) (Ξ» j, β–  (Ξ¦ j))%I.
Proof.
  rewrite /IntoForall /MakeMonPredAt=>HPΦ. rewrite monPred_at_plainly.
  by setoid_rewrite HPΦ.
Qed.

Global Instance is_except_0_monPred_at i P :
  IsExcept0 P β†’ IsExcept0 (P i).
Proof. rewrite /IsExcept0=>- [/(_ i)]. by rewrite monPred_at_except_0. Qed.

Global Instance make_monPred_at_internal_eq `{!BiInternalEq PROP} {A : ofe} (x y : A) i :
  MakeMonPredAt i (x ≑ y) (x ≑ y).
Proof. by rewrite /MakeMonPredAt monPred_at_internal_eq. Qed.
Global Instance make_monPred_at_except_0 i P 𝓠 :
  MakeMonPredAt i P 𝓠 β†’ MakeMonPredAt i (β—‡ P) (β—‡ 𝓠).
Proof. by rewrite /MakeMonPredAt monPred_at_except_0=><-. Qed.
Global Instance make_monPred_at_later i P 𝓠 :
  MakeMonPredAt i P 𝓠 β†’ MakeMonPredAt i (β–· P) (β–· 𝓠).
Proof. by rewrite /MakeMonPredAt monPred_at_later=><-. Qed.
Global Instance make_monPred_at_laterN i n P 𝓠 :
  MakeMonPredAt i P 𝓠 β†’ MakeMonPredAt i (β–·^n P) (β–·^n 𝓠).
Proof. rewrite /MakeMonPredAt=> <-. elim n=>//= ? <-. by rewrite monPred_at_later. Qed.
Global Instance make_monPred_at_fupd `{!BiFUpd PROP} i E1 E2 P π“Ÿ :
  MakeMonPredAt i P π“Ÿ β†’ MakeMonPredAt i (|={E1,E2}=> P) (|={E1,E2}=> π“Ÿ).
Proof. by rewrite /MakeMonPredAt monPred_at_fupd=> <-. Qed.

Global Instance into_internal_eq_monPred_at `{!BiInternalEq PROP}
    {A : ofe} (x y : A) P i :
  IntoInternalEq P x y β†’ IntoInternalEq (P i) x y.
Proof. rewrite /IntoInternalEq=> ->. by rewrite monPred_at_internal_eq. Qed.

Global Instance into_except_0_monPred_at_fwd i P Q 𝓠 :
  IntoExcept0 P Q β†’ MakeMonPredAt i Q 𝓠 β†’ IntoExcept0 (P i) 𝓠.
Proof. rewrite /IntoExcept0 /MakeMonPredAt=> -> <-. by rewrite monPred_at_except_0. Qed.
Global Instance into_except_0_monPred_at_bwd i P π“Ÿ Q :
  IntoExcept0 P Q β†’ MakeMonPredAt i P π“Ÿ β†’ IntoExcept0 π“Ÿ (Q i).
Proof. rewrite /IntoExcept0 /MakeMonPredAt=> H <-. by rewrite H monPred_at_except_0. Qed.

Global Instance maybe_into_later_monPred_at i n P Q 𝓠 :
  IntoLaterN false n P Q β†’ MakeMonPredAt i Q 𝓠 β†’
  IntoLaterN false n (P i) 𝓠.
Proof.
  rewrite /IntoLaterN /MaybeIntoLaterN /MakeMonPredAt=> -> <-. elim n=>//= ? <-.
  by rewrite monPred_at_later.
Qed.
Global Instance from_later_monPred_at i Ο† `(sel : A) n P Q 𝓠 :
  FromModal Ο† (modality_laterN n) sel P Q β†’
  MakeMonPredAt i Q 𝓠 β†’
  FromModal Ο† (modality_laterN n) sel (P i) 𝓠.
Proof.
  rewrite /FromModal /MakeMonPredAt=> HPQ <- ?. rewrite -HPQ //.
  elim n=>//= ? ->.
  by rewrite monPred_at_later.
Qed.

Global Instance frame_monPred_at_later p P 𝓑 𝓠 i :
  Frame p 𝓑 (β–· P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (β–· P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_later. Qed.
Global Instance frame_monPred_at_laterN p n P 𝓑 𝓠 i :
  Frame p 𝓑 (β–·^n P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (β–·^n P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_laterN. Qed.
Global Instance frame_monPred_at_fupd `{!BiFUpd PROP} E1 E2 p P 𝓑 𝓠 i :
  Frame p 𝓑 (|={E1,E2}=> P i) 𝓠 β†’ FrameMonPredAt p i 𝓑 (|={E1,E2}=> P) 𝓠.
Proof. rewrite /Frame /FrameMonPredAt=> ->. by rewrite monPred_at_fupd. Qed.
End bi.
(* When P and/or Q are evars when doing typeclass search on [IntoWand
   (R i) P Q], we use [MakeMonPredAt] in order to normalize the
   result of unification. However, when they are not evars, we want to
   propagate the known information through typeclass search. Hence, we
   do not want to use [MakeMonPredAt].

   As a result, depending on P and Q being evars, we use a different
   version of [into_wand_monPred_at_xx_xx]. *)
Global Hint Extern 3 (IntoWand _ _ (monPred_at _ _) ?P ?Q) =>
     is_evar P; is_evar Q;
     eapply @into_wand_monPred_at_unknown_unknown
     : typeclass_instances.
Global Hint Extern 2 (IntoWand _ _ (monPred_at _ _) ?P (monPred_at ?Q _)) =>
     eapply @into_wand_monPred_at_unknown_known
     : typeclass_instances.
Global Hint Extern 2 (IntoWand _ _ (monPred_at _ _) (monPred_at ?P _) ?Q) =>
     eapply @into_wand_monPred_at_known_unknown_le
     : typeclass_instances.
Global Hint Extern 2 (IntoWand _ _ (monPred_at _ _) (monPred_at ?P _) ?Q) =>
     eapply @into_wand_monPred_at_known_unknown_ge
     : typeclass_instances.

Section modal.
Context {I : biIndex} {PROP : bi}.
Local Notation monPred := (monPred I PROP).
Implicit Types P Q R : monPred.
Implicit Types π“Ÿ 𝓠 𝓑 : PROP.
Implicit Types Ο† : Prop.
Implicit Types i j : I.

Global Instance elim_modal_at_fupd_goal `{!BiFUpd PROP} Ο† p p' E1 E2 E3 π“Ÿ π“Ÿ' Q Q' i :
  ElimModal Ο† p p' π“Ÿ π“Ÿ' (|={E1,E3}=> Q i) (|={E2,E3}=> Q' i) β†’
  ElimModal Ο† p p' π“Ÿ π“Ÿ' ((|={E1,E3}=> Q) i) ((|={E2,E3}=> Q') i).
Proof. by rewrite /ElimModal !monPred_at_fupd. Qed.
Global Instance elim_modal_at_fupd_hyp `{!BiFUpd PROP} Ο† p p' E1 E2 P π“Ÿ π“Ÿ' 𝓠 𝓠' i :
  MakeMonPredAt i P π“Ÿ β†’
  ElimModal Ο† p p' (|={E1,E2}=> π“Ÿ) π“Ÿ' 𝓠 𝓠' β†’
  ElimModal Ο† p p' ((|={E1,E2}=> P) i) π“Ÿ' 𝓠 𝓠'.
Proof. by rewrite /MakeMonPredAt /ElimModal monPred_at_fupd=><-. Qed.

Global Instance elim_acc_at_None `{!BiFUpd PROP} {X} Ο† E1 E2 E3 E4 Ξ± Ξ±' Ξ² Ξ²' P P'x i :
  (βˆ€ x, MakeEmbed (Ξ± x) (Ξ±' x)) β†’ (βˆ€ x, MakeEmbed (Ξ² x) (Ξ²' x)) β†’
  ElimAcc (X:=X) Ο† (fupd E1 E2) (fupd E3 E4) Ξ±' Ξ²' (Ξ» _, None) P P'x β†’
  ElimAcc (X:=X) Ο† (fupd E1 E2) (fupd E3 E4) Ξ± Ξ² (Ξ» _, None) (P i) (Ξ» x, P'x x i).
Proof.
  rewrite /ElimAcc /MakeEmbed. iIntros (HΞ± HΞ² HEA ?) "Hinner Hacc".
  iApply (HEA with "[Hinner]"); first done.
  - iIntros (x).  iSpecialize ("Hinner" $! x). rewrite -HΞ±. by iIntros (? <-).
  - iMod "Hacc". iDestruct "Hacc" as (x) "[HΞ± Hclose]". iModIntro. iExists x.
    rewrite -HΞ± -HΞ². iFrame. iIntros (? _) "HΞ²". by iApply "Hclose".
Qed.
Global Instance elim_acc_at_Some `{!BiFUpd PROP} {X} Ο† E1 E2 E3 E4 Ξ± Ξ±' Ξ² Ξ²' Ξ³ Ξ³' P P'x i :
  (βˆ€ x, MakeEmbed (Ξ± x) (Ξ±' x)) β†’
  (βˆ€ x, MakeEmbed (Ξ² x) (Ξ²' x)) β†’
  (βˆ€ x, MakeEmbed (Ξ³ x) (Ξ³' x)) β†’
  ElimAcc (X:=X) Ο† (fupd E1 E2) (fupd E3 E4) Ξ±' Ξ²' (Ξ» x, Some (Ξ³' x)) P P'x β†’
  ElimAcc (X:=X) Ο† (fupd E1 E2) (fupd E3 E4) Ξ± Ξ² (Ξ» x, Some (Ξ³ x)) (P i) (Ξ» x, P'x x i).
Proof.
  rewrite /ElimAcc /MakeEmbed. iIntros (HΞ± HΞ² HΞ³ HEA ?) "Hinner Hacc".
  iApply (HEA with "[Hinner]"); first done.
  - iIntros (x).  iSpecialize ("Hinner" $! x). rewrite -HΞ±. by iIntros (? <-).
  - iMod "Hacc". iDestruct "Hacc" as (x) "[HΞ± Hclose]". iModIntro. iExists x.
    rewrite -HΞ± -HΞ² -HΞ³. iFrame. iIntros (? _) "HΞ² /=". by iApply "Hclose".
Qed.

Global Instance add_modal_at_fupd_goal `{!BiFUpd PROP} E1 E2 π“Ÿ π“Ÿ' Q i :
  AddModal π“Ÿ π“Ÿ' (|={E1,E2}=> Q i) β†’ AddModal π“Ÿ π“Ÿ' ((|={E1,E2}=> Q) i).
Proof. by rewrite /AddModal !monPred_at_fupd. Qed.

(* This hard-codes the fact that ElimInv with_close returns a
   [(Ξ» _, ...)] as Q'. *)
Global Instance elim_inv_embed_with_close {X : Type} Ο†
    π“Ÿinv π“Ÿin (π“Ÿout π“Ÿclose : X β†’ PROP)
    Pin (Pout Pclose : X β†’ monPred)
    Q Q' :
  (βˆ€ i, ElimInv Ο† π“Ÿinv π“Ÿin π“Ÿout (Some π“Ÿclose) (Q i) (Ξ» _, Q' i)) β†’
  MakeEmbed π“Ÿin Pin β†’ (βˆ€ x, MakeEmbed (π“Ÿout x) (Pout x)) β†’
  (βˆ€ x, MakeEmbed (π“Ÿclose x) (Pclose x)) β†’
  ElimInv (X:=X) Ο† βŽ‘π“Ÿinv⎀ Pin Pout (Some Pclose) Q (Ξ» _, Q').
Proof.
  rewrite /MakeEmbed /ElimInv=>H <- Hout Hclose ?. iStartProof PROP.
  setoid_rewrite <-Hout. setoid_rewrite <-Hclose.
  iIntros (?) "(?&?&HQ')". iApply H; [done|]. iFrame. iIntros (x) "?".
  by iApply "HQ'".
Qed.
Global Instance elim_inv_embed_without_close  {X : Type}
    Ο† π“Ÿinv π“Ÿin (π“Ÿout : X β†’ PROP) Pin (Pout : X β†’ monPred) Q (Q' : X β†’ monPred) :
  (βˆ€ i, ElimInv Ο† π“Ÿinv π“Ÿin π“Ÿout None (Q i) (Ξ» x, Q' x i)) β†’
  MakeEmbed π“Ÿin Pin β†’ (βˆ€ x, MakeEmbed (π“Ÿout x) (Pout x)) β†’
  ElimInv (X:=X) Ο† βŽ‘π“Ÿinv⎀ Pin Pout None Q Q'.
Proof.
  rewrite /MakeEmbed /ElimInv=>H <-Hout ?. iStartProof PROP.
  setoid_rewrite <-Hout.
  iIntros (?) "(?&?&HQ')". iApply H; [done|]. iFrame. iIntros (x) "?".
  by iApply "HQ'".
Qed.
End modal.