Guarantees by Construction (Mechanization)

Jules Jacobs

  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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
From cgraphs.multiparty Require Import invariant langdef.
Require Import Coq.Logic.Classical.


Definition blocked es h (x y : object) (l : clabel) : Prop :=
   i j, x = Thread i  y = Chan j  thread_blocked es h i j.

Lemma rtyped_inner e t :                                                         (* https://apndx.org/pub/thesis/thesis.pdf#nameddest=20167ff6 *)
  rtyped0 e t -  (∃ v, e = Val v)  
   k e0, ctx k  e = k e0 
    ((∃ e', pure_step e0 e') 
     (∃ v p, e0 = Recv p (Val v)) 
     (∃ v1 v2 p i, e0 = Send p (Val v1) i (Val v2)) 
     (∃ n f, e0 = Spawn n (Val  f)) 
     (∃ v, e0 = Close (Val v))) .
Proof.
  iIntros "H".
  iInduction e as [] "IH" forall (t); simpl; [eauto|eauto|..].
  - iDestruct "H" as (t1 t2 ->) "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    iDestruct ("IH1" with "H2") as "%". iClear "IH1".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + destruct H0 as [[v' ->]|(k & e0 & Hk & -> & H0)].
      * iPureIntro. right. exists  x, x). eexists.
        split_and!; eauto.
        { constructor. }
        left. eexists.
        constructor.
      * iPureIntro. right.
        eexists  x, Pair (Val v) (k x)),_.
        split_and!; eauto.
        constructor; eauto. constructor.
    + iPureIntro. right.
      eexists  x, Pair (k x) e2),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, Pair x e2)); eauto. constructor.
  - iDestruct "H" as (t1 t2 ->) "H".
    iDestruct ("IH" with "H") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + iPureIntro. right. exists  x, x). eexists.
      split_and!; eauto.
      { constructor. }
      left. eexists.
      constructor.
    + iPureIntro. right.
      eexists  x, Inj b (k x)),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, Inj b x)); eauto.
      constructor.
  - iDestruct "H" as (n0 f i' [-> ->]) "H".
    iDestruct ("IH" with "H") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + iPureIntro. right. exists  x, x). eexists.
      split_and!; eauto.
      { constructor. }
      left. eexists.
      constructor.
    + iPureIntro. right.
      eexists  x, InjN i' (k x)),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, InjN i' x)); eauto.
      econstructor.
  - iDestruct "H" as (t') "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    iDestruct ("IH1" with "H2") as "%". iClear "IH1".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + destruct H0 as [[v' ->]|(k & e0 & Hk & -> & H0)].
      * simpl. rewrite val_typed_val_typed'. simpl.
        iDestruct "H1" as (x e ->) "H1".
        iPureIntro. right. exists  x, x). eexists.
        split_and!; eauto.
        { constructor. }
        left. eexists.
        constructor.
      * iPureIntro. right.
        eexists  x, App (Val v) (k x)),_.
        split_and!; eauto.
        constructor; eauto. constructor.
    + iPureIntro. right.
      eexists  x, App (k x) e2),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, App x e2)); eauto.
      constructor.
  - iDestruct "H" as (t') "[H1 H2]".
      iDestruct ("IH" with "H1") as "%". iClear "IH".
      iDestruct ("IH1" with "H2") as "%". iClear "IH1".
      destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
      + destruct H0 as [[v' ->]|(k & e0 & Hk & -> & H0)].
        * simpl. rewrite val_typed_val_typed'. simpl.
          iDestruct "H1" as (x e ->) "H1".
          iPureIntro. right. exists  x, x). eexists.
          split_and!; eauto.
          { constructor. }
          left. eexists.
          constructor.
        * iPureIntro. right.
          eexists  x, UApp (Val v) (k x)),_.
          split_and!; eauto.
          constructor; eauto. constructor.
      + iPureIntro. right.
        eexists  x, UApp (k x) e2),_.
        split_and!; eauto.
        eapply (Ctx_cons  x, UApp x e2)); eauto.
        constructor.
  - iPureIntro. right.
    eexists  x, x),_.
    split_and!; [constructor|eauto|].
    left. eexists. constructor.
  - iPureIntro. right.
    eexists  x, x),_.
    split_and!; [constructor|eauto|].
    left. eexists. constructor.
  - iDestruct "H" as (n' r t' i [-> ->]) "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    iDestruct ("IH1" with "H2") as "%". iClear "IH1".
    iPureIntro.
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + destruct H0 as [[v' ->]|(k & e0 & Hk & -> & H0)].
      * right.
        eexists  x, x), _.
        split_and!; [constructor|eauto 10..].
      * right.
        eexists  x, Send p (Val v) _ (k x)),_.
        split_and!; eauto.
        constructor; eauto. constructor.
    + right.
      eexists  x, Send p (k x) _ e2),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, Send p x _ e2)); eauto.
      constructor.
  - iDestruct "H" as (n r' r Q) "H".
    iDestruct ("IH" with "H") as "%". iClear "IH".
    iPureIntro. right.
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + eexists  x, x),_. split_and!; [constructor|eauto 10..].
    + eexists  x, Recv p (k x)),_. split_and!; eauto.
      constructor; eauto. constructor.
  - iDestruct "H" as (t') "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + iPureIntro. right.
      eexists  x, x), _. split_and!; [constructor|eauto|].
      left. eexists. constructor.
    + iPureIntro. right.
      eexists  x, Let s (k x) e2),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, Let s x e2)); eauto.
      constructor.
  - iDestruct "H" as "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl.
      iDestruct "H1" as "->". iPureIntro. right.
      eexists  x, x), _. split_and!; [constructor|eauto|].
      left. eexists. constructor.
    + iPureIntro. right.
      eexists  x, LetUnit (k x) e2),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, LetUnit x e2)); eauto.
      constructor.
  - iDestruct "H" as (t1 t2 Hneq) "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl.
      iDestruct "H1" as (a b ->) "[H11 H12]". iPureIntro. right.
      eexists  x, x), _. split_and!; [constructor|eauto|].
      left. eexists. constructor.
    + iPureIntro. right.
      eexists  x, LetProd s s0 (k x) e2),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, LetProd s s0 x e2)); eauto.
      constructor.
  - iDestruct ("IH" with "H") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl. iDestruct "H" as %[].
    + iPureIntro. right.
      eexists  x, MatchVoid (k x)),_. split_and!; eauto.
      constructor; eauto. constructor.
  - iDestruct "H" as (t1 t2) "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl.
      iDestruct "H1" as (b a) "[-> H]".
      iPureIntro. right.
      exists  x, x). eexists.
      split_and!; eauto.
      { constructor. }
      left.
      eexists. constructor.
    + iPureIntro. right.
      eexists  x, MatchSum (k x) s e2 e3),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, MatchSum x s e2 e3)); eauto.
      constructor.
  - iDestruct "H" as (f) "[H1 H2]".
    iDestruct ("IH1" with "H1") as "%". iClear "IH IH1".
    destruct H as [[v ->]|(k & e0' & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl.
      iDestruct "H1" as (i a) "[-> H1]".
      iPureIntro. right. exists  x, x). eexists.
      split_and!; eauto using ctx, pure_step.
    + iPureIntro. right.
      eexists  x, MatchSumN n (k x) e0),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, MatchSumN n x e0)); eauto.
      constructor.
  - iDestruct "H" as "[H1 H2]".
    iDestruct ("IH" with "H1") as "%". iClear "IH".
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl.
      iDestruct "H1" as (n) "->".
      iPureIntro. right. exists  x, x). eexists.
      split_and!; eauto.
      { constructor. }
      left.
      destruct (decide (n = 0)); subst; eexists.
      * eapply If_step2.
      * constructor. done.
    + iPureIntro. right.
      eexists  x, If (k x) e2 e3),_.
      split_and!; eauto.
      eapply (Ctx_cons  x, If x e2 e3)); eauto.
      constructor.
  - iDestruct "H" as (σs [Hteq Hcons]) "H".
    destruct (classic (∀ i,  v, e i = Val v)) as [H|H].
    + iPureIntro. right.
      exists id, (Spawn n e).
      split; first eauto using ctx.
      split; first done.
      right. right. right. left.
      exists n.
      eapply fin_choice in H as [f Hf].
      exists f. f_equiv.
      apply functional_extensionality; eauto.
    + destruct (classic (∃ i,  v, e i  Val v)); last first.
      { exfalso.
        assert (∀ i, ¬  v, e i  Val v) by naive_solver.
        eapply H. intros. specialize (H1 i).
        destruct (classic (∃ v, e i = Val v)); eauto.
        exfalso. eapply H1. intros ??.
        eapply H2. exists v. done. }
      destruct H0 as [i Hi].
      iRight.
      iDestruct (big_sepS_elem_of_acc with "H") as "[H1 H2]".
      { eapply all_fin_all. }
      iDestruct ("IH" with "H1") as %HH. iPureIntro.
      destruct HH; first naive_solver.
      destruct H0 as (k & e0 & Hk & He & HH).
      eexists ((λ x, Spawn n  j, if decide (i = j) then x else e j))  k),_.
      split_and!; eauto.
      * eapply (Ctx_cons  x, Spawn n  j, if decide (i = j) then x else e j)) k);
        eauto using ctx1.
      * simpl. f_equal. eapply functional_extensionality. intro.
        case_decide; subst; eauto.
  - iDestruct "H" as (->) "H".
    iDestruct ("IH" with "H") as "%". iClear "IH".
    iPureIntro. right.
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + eexists  x, x),_. split_and!; [constructor|eauto 10..].
    + eexists  x, Close (k x)),_. split_and!; eauto.
      constructor; eauto. constructor.
  - iDestruct "H" as (σ ->) "H".
    iDestruct ("IH" with "H") as "%". iClear "IH".
    iRight.
    destruct H as [[v ->]|(k & e0 & Hk & -> & H)].
    + simpl. rewrite val_typed_val_typed'. simpl.
      iDestruct "H" as (c π ->) "H".
      iPureIntro.
      eexists  x, x),_.
      split_and!; eauto using ctx.
      destruct c. eauto using pure_step.
    + iPureIntro.
      eexists  x, Relabel p (k x)),_. split_and!; eauto.
      constructor; eauto. constructor.
Qed.

Lemma heap_fresh (h : heap) :
   i,  p, h !! (i,p) = None.
Proof.
  exists (fresh (dom (gmap_curry h))).
  intro. pose proof (is_fresh (dom (gmap_curry h))).
  rewrite ->not_elem_of_dom in H.
  rewrite -lookup_gmap_curry.
  rewrite H. done.
Qed.

Lemma final_state_decision (es : list expr) (h : heap) :
  ((∃ c, is_Some (h !! c))  (∃ e, e  es  e  Val UnitV)) 
  (h =    e, e  es -> e = Val UnitV).
Proof.
  destruct (classic ((∃ c, is_Some (h !! c))  (∃ e, e  es  e  Val UnitV))); eauto.
  right. split.
  - apply map_eq. intros. rewrite lookup_empty.
    destruct (h !! i) eqn:E; eauto. exfalso.
    apply H. left. eexists. erewrite E. eauto.
  - intros.
    assert  (e  Val UnitV)) by naive_solver.
    by apply NNPP.
Qed.



Definition own_dom A : rProp :=  Σ, ⌜⌜ A = dom Σ ⌝⌝  own Σ.

Lemma own_dom_empty : own_dom  ⊣⊢ emp.
Proof.
  iSplit; unfold own_dom; iIntros "H".
  - iDestruct "H" as (? H) "H".
    symmetry in H. apply dom_empty_iff_L in H as ->.
    by iApply own_empty.
  - iExists . rewrite own_empty dom_empty_L //.
Qed.

Lemma own_dom_singleton k v : own {[ k := v ]}  own_dom {[ k ]}.
Proof.
  iIntros "H". iExists {[ k := v ]}.
  rewrite dom_singleton_L. iFrame. done.
Qed.

Lemma own_dom_union A B : own_dom A  own_dom B  own_dom (A  B).
Proof.
  iIntros "[H1 H2]".
  iDestruct "H1" as (Σ1 H1) "H1".
  iDestruct "H2" as (Σ2 H2) "H2". subst.
  iExists (Σ1  Σ2). rewrite dom_union_L. iSplit; eauto.
  iApply own_union. iFrame.
Qed.

Lemma own_dom_fin_gset `{Countable A} n (g : fin n -> A) (f : A -> gset object) :
  ([ set] p  fin_gset n g, own_dom (f p)) - own_dom (big_union (fin_gset n (f  g))).
Proof.
  induction n.
  - rewrite !fin_gset_0 big_union_empty big_sepS_empty own_dom_empty //.
  - rewrite !fin_gset_S big_union_singleton_union.
    destruct (decide (g 0%fin  fin_gset n  i : fin n, g (FS i)))).
    + rewrite subseteq_union_1_L; last rewrite singleton_subseteq_l //.
      rewrite subseteq_union_1_L; first apply IHn.
      eapply elem_of_fin_gset in e.
      intros ??.
      eapply elem_of_big_union.
      destruct e. simpl in *.
      rewrite -H1 in H0.
      eexists. split; last done.
      eapply elem_of_fin_gset. eauto.
    + rewrite big_sepS_insert //.
      iIntros "[H1 H2]".
      iDestruct (IHn with "H2") as "H2".
      iApply own_dom_union. iFrame.
Qed.

Lemma own_dom_fin_union n f :
  ([ set] p  all_fin n, own_dom (f p))  own_dom (fin_union n f).
Proof.
  iApply own_dom_fin_gset.
Qed.

Ltac model := repeat
  (setoid_rewrite pure_sep_holds || setoid_rewrite exists_holds
  || setoid_rewrite own_holds || setoid_rewrite val_typed_val_typed'
  || setoid_rewrite sep_holds).

Lemma own_dom_all {A} (f : A -> gset object) :
  (∀ i, own_dom (f i))    i j, f i = f j .
Proof.
  apply entails_holds.
  intros Σ H.
  rewrite pure_holds. intros.
  rewrite ->forall_holds in H.
  assert (∀ i, f i = dom Σ).
  { intros k. specialize (H k).
    eapply exists_holds in H as [].
    eapply pure_sep_holds in H as [].
    eapply own_holds in H0.
    rewrite -H0 H //. }
  rewrite !H0 //.
Qed.

Lemma own_dom_and A B :
  own_dom A  own_dom B   A = B .
Proof.
  iIntros "H".
  iAssert (∀ c:bool, own_dom (if c then A else B))%I with "[H]" as "H".
  { iIntros ([]).
    - by iDestruct "H" as "[H _]".
    - by iDestruct "H" as "[_ H]". }
  iDestruct (own_dom_all with "H") as %Q.
  specialize (Q true false). simpl in *. eauto.
Qed.

Lemma fin_union_same `{Countable A} n (s : gset A) :
  fin_union (S n)  i, s) = s.
Proof.
  induction n.
  - rewrite fin_union_S fin_union_0 right_id_L //.
  - rewrite fin_union_S IHn union_idemp_L //.
Qed.

Lemma rtyped_refs Γ e t :
  rtyped Γ e t  own_dom (expr_refs e)
with val_typed_refs v t :
  val_typed v t  own_dom (val_refs v).
Proof.
  - iIntros "H". destruct e; simpl; repeat (iDestruct "H" as (?) "H");
    rewrite ?val_typed_refs ?rtyped_refs ?own_dom_empty ?own_dom_union; eauto.
    + iDestruct "H" as "[H1 H2]".
      rewrite -assoc_L.
      iApply own_dom_union. iFrame.
      iDestruct (own_dom_and with "H2") as %->.
      iDestruct "H2" as "[_ H2]".
      rewrite union_idemp_L //.
    + iDestruct "H" as "[H1 H2]". iApply own_dom_union; iFrame.
      case_decide; subst. { rewrite fin_union_0 own_dom_empty //. }
      iAssert (∀ i, own_dom (expr_refs (e0 i)))%I with "[H2]" as "H".
      { iIntros (i). iApply rtyped_refs. eauto. }
      destruct n; simplify_eq.
      iDestruct (own_dom_all with "H") as %Q.
      assert (expr_refs  e0 = λ i, expr_refs (e0 0%fin)) as ->.
      { apply functional_extensionality. intros. eapply Q. }
      rewrite fin_union_same. iApply "H".
    + iDestruct "H" as "[H1 [H2 _]]"; iApply own_dom_union; iFrame.
    + iApply own_dom_fin_union.
      iApply (big_sepS_impl with "H"). iModIntro.
      iIntros (x Hx) "H". simpl.
      iApply rtyped_refs. done.
  - iIntros "H". destruct v; simpl; rewrite ?own_dom_empty; eauto;
    repeat (iDestruct "H" as (?) "H"); rewrite ?val_typed_refs ?rtyped_refs ?own_dom_union; eauto.
    destruct e. by iApply own_dom_singleton.
Qed.

Lemma own_dom_map_union `{Countable A} {V V'} (ma : gmap A V) (mb : gmap A V') f :
  ([ map] a;b  ma;mb, own_dom (f a)) -
  own_dom (map_union f ma).
Proof.
  revert mb; induction ma using map_ind; intros; iIntros "H".
  - iDestruct (big_sepM2_empty_r with "H") as %->.
    iClear "H". unfold map_union. rewrite map_fold_empty own_dom_empty //.
  - iDestruct (big_sepM2_lookup_iff with "H") as %Q.
    assert (is_Some (mb !! i)) as []. { rewrite -Q; smap. }
    assert (mb = <[ i := x0 ]> mb) as -> by (apply map_eq; intro; smap).
    rewrite big_sepM2_insert_delete.
    iDestruct "H" as "[H1 H2]".
    rewrite delete_notin //.
    iDestruct (IHma with "H2") as "H2".
    unfold map_union. rewrite map_fold_insert; eauto.
    { iApply own_dom_union. iFrame. }
    intros ??????. smap; intros; smap; set_solver.
Qed.

Lemma bufs_typed_refs bufss σs :
  bufs_typed bufss σs  own_dom (bufs_refs bufss).
Proof.
  iIntros "H".
  unfold bufs_typed.
  iDestruct "H" as (sbufs Hsbufs) "H".
  unfold entries_typed.
  unfold bufs_refs.
  iApply own_dom_map_union.
  iApply (big_sepM2_impl with "H"). iModIntro.
  iIntros (k x1 x2 H1 H2) "H".
  iApply own_dom_map_union.
  iApply (big_sepM2_impl with "H"). iModIntro.
  iIntros (k' l1 l2 H1' H2') "H".
  unfold buf_refs. clear.
  iInduction l1 as [] "IH" forall (l2); simpl.
  - iDestruct (big_sepL2_nil_inv_l with "H") as %->.
    rewrite own_dom_empty //.
  - destruct a.
    destruct l2.
    { iDestruct (big_sepL2_nil_inv_r with "H") as %Q. smap. }
    simpl. iDestruct "H" as "[[% H1] H2]".
    iApply own_dom_union.
    iSplitL "H1".
    + iApply val_typed_refs. eauto.
    + iApply "IH". iFrame.
Qed.

Lemma obj_refs_state_inv' es h x Δ :
  state_inv es h x Δ  own_dom (obj_refs es h x).
Proof.
  iIntros "H".
  destruct x; simpl.
  - iDestruct "H" as (?) "H". destruct (es !! n); simpl;
    rewrite -?rtyped_rtyped0_iff ?rtyped_refs ?own_dom_empty //.
  - iDestruct "H" as (σs H) "H".
    iApply bufs_typed_refs. done.
Qed.

Lemma obj_refs_state_inv es h x Δ Σ :
  holds (state_inv es h x Δ) Σ -> obj_refs es h x = dom Σ.
Proof.
  intros HH. eapply holds_entails in HH; last apply obj_refs_state_inv'.
  revert HH. model. intros (Σ' & HH1 & HH2). rewrite HH1 HH2 //.
Qed.


Lemma dom_lookup_Some_equiv `{Countable A} `{Equiv B} (m : gmap A B) (x : A) (y : B) :
  m !! x  Some y -> x  dom m.
Proof.
  intros HH. inversion HH. subst. eapply elem_of_dom. rewrite -H1 //.
Qed.

Lemma gmap_slice_pop_fmap `{Countable A,Countable B,Countable C} {V}
  (c : A) (p : B) (q : C) (m : bufsT B (A*C) V) :
  pop p q (gmap_slice m c) =  '(x,m'), (x,gmap_slice m' c)) <$> pop p (c,q) m.
Proof.
  unfold pop. rewrite gmap_slice_lookup.
  destruct (m !! (c, q)); eauto.
  destruct (g !! p); eauto.
  destruct l; eauto. simpl.
  rewrite gmap_slice_insert. smap.
Qed.

Lemma strong_progress es h x :                                                   (* https://apndx.org/pub/thesis/thesis.pdf#nameddest=d8b9a415 *)
  invariant es h -> active es h x -> reachable es h x.
Proof.
  intros Hinv. assert (invariant es h) as Hinv'; eauto.
  revert Hinv'.
  intros (g & Hwf & Hvs). revert x.
  eapply (cgraph_ind' (blocked es h) g  x,
    active es h x  reachable es h x));
    [solve_proper|eauto|].
  intros x Hind_out Hind_in Hactive.
  (* Get the invariant for x *)
  pose proof (Hvs x) as Hx.
  (* Case analyze whether x is a channel or a thread *)
  destruct x as [i|c]; simpl in *.
  - (* Thread *)
    destruct Hactive as (e & He & Heneq). (* Thread is active, so must have expression in thread pool *)
    rewrite He in Hx. (* We can conclude that this expression is well-typed wrt out edges *)
    apply pure_sep_holds in Hx as [Hinl Hx].
    eapply prim_simple_adequacy in Hx as Hx'; last eapply rtyped_inner.
    (* Case analyze whether it's a value or pure step or channel op  *)
    destruct Hx' as [(v & ->)|Hx'].
    {
      (* Value *)
      eapply prim_simple_adequacy; first done.
      simpl. rewrite val_typed_val_typed'. simpl.
      iIntros (->). simplify_eq.
    }
    (* Expr in context *)
    destruct Hx' as (k' & e0 & Hctx & -> & Hcases).
    rewrite ->rtyped0_ctx in Hx; eauto.
    apply exists_holds in Hx as [t Hx].
    apply sep_holds in Hx as (Σ1&Σ2&Hout&Hdisj&Het&Hctxt).
    destruct Hcases as [H|[H|[H|[H|H]]]].
    * (* Pure step *)
      destruct H as [e' H].
      eapply Thread_step_reachable.
      eexists _,_.
      econstructor; last done.
      eauto using head_step, ctx_step.
    * (* Recv *)
      destruct H as (v & p & ->).
      revert Het.
      model.
      intros (n & t' & r & Q & [c q] & π & -> & Het). simpl in *.

      assert (out_edges g (Thread i) !! Chan c  Some (q, RecvT n (π p) t' (relabelT π  r))) as HH.
      {
        rewrite Hout -Het.
        erewrite lookup_union_Some_l; last first.
        - rewrite lookup_singleton. done.
        - do 2 f_equiv. apply session_type_equiv_alt. done.
      }

      pose proof (out_edges_in_labels _ _ _ _ HH) as [x Hin].

      pose proof (Hvs (Chan c)) as Hc.
      revert Hc. rewrite Hin. intros Hc.
      simpl in *.
      eapply exists_holds in Hc as [σs Hc].
      eapply pure_sep_holds in Hc as [Heq Hc].
      eapply map_to_multiset_lookup in Heq.
      (* eapply prim_simple_adequacy; first exact Hc. *)
      (* iIntros "H". *)
      (* iDestruct (bufs_typed_recv with "H") as %(bufs & buf & Hbufs & Hbuf); first done. *)
      (* iPureIntro. *)
      destruct (pop (π p) (c,q) h) as [[[]]|] eqn:E.
      {
        eapply Thread_step_reachable.
        unfold can_stepi.
        eexists _,_.
        econstructor; last done.
        econstructor; first done.
        eapply Recv_step; eauto.
      }
      assert (thread_blocked es h i c).
      { unfold thread_blocked.
        assert (is_Some (h !! (c,q))); last eauto 10.
        eapply prim_simple_adequacy; first exact Hc.
        iIntros "H".
        iDestruct (bufs_typed_recv with "H") as %QQ.
        {
          inversion Heq. simplify_eq.
          eexists. eauto.
        }
        iPureIntro. rewrite gmap_slice_lookup in QQ.
        done. }
      eapply Thread_blocked_reachable; eauto.
      eapply Hind_out. { unfold blocked; eauto. }
      eexists _,_; eauto.
      unfold active.
      exists q.
      rewrite -gmap_slice_lookup.
      eapply prim_simple_adequacy; first exact Hc.
      eapply bufs_typed_recv; eauto.
      inversion Heq. rewrite -H0. eauto.
    * (* Send *)
      destruct H as (v1 & v2 & p & i' & ->).
      revert Het. model.
      intros (j & r & t' & j' & [-> ->] & Σ3 & Σ4 & Σeq & Hdisj' & ([c b] & π & -> & Het1) & Het2).
      eapply Thread_step_reachable. eexists _,_.
      econstructor; last done.
      eauto using head_step, ctx_step.
    * (* Fork *)
      destruct H as (n & f & ->).
      destruct (heap_fresh h) as [ii HH].
      eapply Thread_step_reachable. eexists _,_.
      econstructor; last done.
      eauto using head_step, ctx_step.
    * (* Close *)
      destruct H as (v & ->).
      revert Het. model.
      intros (-> & ([c b] & π & -> & Het)).
      eapply Thread_step_reachable. eexists _,_.
      econstructor; last done.
      eauto using head_step, ctx_step.
  - (* Channel *)
    destruct Hactive as (b & Hib).
    rewrite -gmap_slice_lookup in Hib.
    apply exists_holds in Hx as [σs Hx].
    apply pure_sep_holds in Hx as [Hinl Hx].
    eapply prim_simple_adequacy; first exact Hx.
    iIntros "H".
    iDestruct (bufs_typed_progress with "H") as %[HH|Hcp].
    { rewrite HH lookup_empty in Hib. by destruct Hib. }
    iPureIntro.
    unfold can_progress in *.
    destruct Hcp as (p & σ & Hp & ).
    (* destruct Hp as (σ & bufs & Hσ & Hbufs & Hrne). *)

    assert (∃ x, out_edges g x !! (Chan c)  Some (p, σ)) as [y Hy].
    {
      erewrite map_to_multiset_Some in Hinl; last done.
      eapply in_labels_out_edges; eauto.
    }

    eapply (Chan_ref_reachable _ _ _ y).
    {
      erewrite obj_refs_state_inv; eauto.
      eapply dom_lookup_Some_equiv; eauto.
    }

    eapply Hind_in; eauto.
    + intros (i & c' & -> & ? & Hw). simplify_eq.
      unfold thread_blocked in Hw.
      destruct Hw as (p' & q' & k & π & Hctx & Hi & Hpres & Hpop).
      specialize (Hvs (Thread i)).
      eapply (holds_entails _ (∃ n t s, own_out (Chan c') (q', RecvT n (π p') t (relabelT π  s))  True)%I) in Hvs. 2:
      {
        simpl. iIntros "[_ H]". rewrite Hi.
        rewrite rtyped0_ctx //.
        iDestruct "H" as (t) "[H1 H2]". simpl.
        iDestruct "H1" as (? t' r ? Q) "H1".
        remember (SumNT n  i : fin n, PairT (ChanT (r i)) (t' i))).
        inversion H; simplify_eq.
        iDestruct "H1" as (HH) "H". simplify_eq.
        iExists _,_,_. unfold own_ep. simpl.
        rewrite -(session_type_id_id (relabelT π (RecvT n p' t' r))) /=. iFrame.
      }
      apply exists_holds in Hvs as [n Hvs].
      apply exists_holds in Hvs as [tt Hvs].
      apply exists_holds in Hvs as [ss Hvs].
      assert (out_edges g (Thread i) !! Chan c'  Some (q', RecvT n (π p') tt (relabelT π  ss))) as Hoc'.
      {
        eapply sep_holds in Hvs as (Σ1 & Σ2 & H1 & HD & [HH _]).
        rewrite H1.
        eapply own_holds in HH.
        rewrite lookup_union -HH lookup_singleton.
        destruct (Σ2 !! Chan c') eqn:E; rewrite E; simpl; done.
      }
      revert Hoc'. rewrite Hy. intros Hoc'.
      inversion Hoc'. simplify_eq.
      (* inversion H1. simpl in *. *)
      (* inversion H0; simplify_eq. *)
      destruct  as (y & bufs' & ).
      rewrite gmap_slice_pop_fmap Hpop in .
      simplify_eq.
    + specialize (Hvs y).
      revert Hy Hvs. clear.
      intros Hy Hvs.
      destruct y; simpl in *.
      { eapply pure_sep_holds in Hvs as [_ Hvs].
        destruct (es !! n).
        - eexists. split; first done.
          intros ->. simpl in *.
          eapply affinely_pure_holds in Hvs as [Hvs _].
          revert Hy. rewrite Hvs lookup_empty. intros HH.
          inversion HH.
        - eapply emp_holds in Hvs. exfalso.
          revert Hy. rewrite Hvs lookup_empty. intros HH.
          inversion HH.
      }
      {
        eapply exists_holds in Hvs as [σs Hvs].
        eapply pure_sep_holds in Hvs as [_ Hvs].
        destruct (classic (∃ p : participant, is_Some (h !! (s, p)))); eauto.
        exfalso.
        assert (gmap_slice h s = ) as HH.
        { eapply map_eq. intros x.
          rewrite lookup_empty gmap_slice_lookup.
          destruct (h !! (s,x)) eqn:E; eauto.
          exfalso. eauto. }
        rewrite HH in Hvs.
        eapply holds_entails in Hvs; last apply bufs_typed_empty_inv.
        eapply affinely_pure_holds in Hvs as [Hvs _].
        revert Hy. rewrite Hvs lookup_empty. intros HHH.
        inversion HHH.
      }
Qed.

Lemma active_progress es h x :
  invariant es h -> active es h x ->  (es' : list expr) (h' : heap), step es h es' h'.
Proof.
  intros H1 H2.
  cut (reachable es h x); eauto using strong_progress. clear.
  induction 1; eauto. destruct H as (es'&h'&?). exists es', h'. econstructor; eauto.
Qed.

Lemma inv_global_progress es h :
  invariant es h ->
  (h =    e, e  es -> e = Val UnitV) 
  (∃ es' h', step es h es' h').
Proof.
  intros H.
  destruct (final_state_decision es h) as [Hdec|Hdec]; eauto; right.
  assert (∃ x, active es h x) as [x Hactive].
  { destruct Hdec as [(x&?)|(x&?)].
    + destruct x as [c b]. exists (Chan c). simpl. eauto.
    + destruct H0. eapply elem_of_list_lookup in H0 as [].
      exists (Thread x0). simpl. eauto. }
  eapply active_progress; eauto.
Qed.

Lemma activeset_exists es h :
   s : gset object,  x, active es h x -> x  s.
Proof.
  exists (list_to_set (Thread <$> seq 0 (length es)) 
          set_map (Chan  fst) (dom h)).
  intros. rewrite elem_of_union.
  rewrite elem_of_list_to_set elem_of_list_fmap elem_of_map.
  setoid_rewrite elem_of_seq.
  setoid_rewrite elem_of_dom. simpl.
  unfold active in *.
  destruct x;[left|right].
  - destruct H as (?&?&?). eapply lookup_lt_Some in H. eauto with lia.
  - destruct H as (?&?). eexists (_,_). eauto.
Qed.

Lemma obj_refs_active es h x y :
  y  obj_refs es h x -> active es h x.
Proof.
  destruct x; simpl.
  - destruct (es !! n); simpl; last set_solver.
    destruct (classic (e = Val UnitV)); eauto.
    subst. simpl. set_solver.
  - destruct (decide (gmap_slice h s = )) as [->|]; first set_solver.
    apply map_choose in n as (?&?&?).
    rewrite gmap_slice_lookup in H. eauto.
Qed.