joining_existentials.v 4.25 KB
Newer Older
1 2
From iris.program_logic Require Export weakestpre hoare.
From iris.heap_lang Require Export lang.
3
From iris.algebra Require Import excl agree csum.
Ralf Jung's avatar
Ralf Jung committed
4
From iris.heap_lang.lib.barrier Require Import proof specification.
Robbert Krebbers's avatar
Robbert Krebbers committed
5 6
From iris.heap_lang Require Import notation par proofmode.
From iris.proofmode Require Import invariants.
Ralf Jung's avatar
Ralf Jung committed
7

8 9 10 11
Definition one_shotR (Σ : gFunctors) (F : cFunctor) :=
  csumR (exclR unitC) (agreeR $ laterC $ F (iPreProp Σ)).
Definition Pending {Σ F} : one_shotR Σ F := Cinl (Excl ()).
Definition Shot {Σ} {F : cFunctor} (x : F (iProp Σ)) : one_shotR Σ F :=
12 13
  Cinr $ to_agree $ Next $ cFunctor_map F (iProp_fold, iProp_unfold) x.

14 15
Class oneShotG (Σ : gFunctors) (F : cFunctor) :=
  one_shot_inG :> inG Σ (one_shotR Σ F).
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
16 17
Definition oneShotGF (F : cFunctor) : gFunctor :=
  GFunctor (csumRF (exclRF unitC) (agreeRF ( F))).
18
Instance inGF_oneShotG  `{inGF Σ (oneShotGF F)} : oneShotG Σ F.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
19 20
Proof. apply: inGF_inG. Qed.

21
Definition client eM eW1 eW2 : expr :=
Robbert Krebbers's avatar
Robbert Krebbers committed
22
  let: "b" := newbarrier #() in
23
  (eM ;; signal "b") || ((wait "b" ;; eW1) || (wait "b" ;; eW2)).
24
Global Opaque client.
Ralf Jung's avatar
Ralf Jung committed
25 26

Section proof.
27
Context `{!heapG Σ, !barrierG Σ, !spawnG Σ, !oneShotG Σ F}.
28
Context (N : namespace).
29
Local Notation X := (F (iProp Σ)).
Ralf Jung's avatar
Ralf Jung committed
30

31
Definition barrier_res γ (Φ : X  iProp Σ) : iProp Σ :=
32
  ( x, own γ (Shot x)  Φ x)%I.
Ralf Jung's avatar
Ralf Jung committed
33

34
Lemma worker_spec e γ l (Φ Ψ : X  iProp Σ) `{!Closed [] e} :
35
  recv N l (barrier_res γ Φ)  ( x, {{ Φ x }} e {{ _, Ψ x }})
36
   WP wait #l ;; e {{ _, barrier_res γ Ψ }}.
Ralf Jung's avatar
Ralf Jung committed
37
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
38
  iIntros "[Hl #He]". wp_apply wait_spec; iFrame "Hl".
39
  iDestruct 1 as (x) "[#Hγ Hx]".
40
  wp_seq. iApply wp_wand_l. iSplitR; [|by iApply "He"].
41
  iIntros (v) "?"; iExists x; by iSplit.
Ralf Jung's avatar
Ralf Jung committed
42 43
Qed.

44
Context (P : iProp Σ) (Φ Φ1 Φ2 Ψ Ψ1 Ψ2 : X -n> iProp Σ).
Robbert Krebbers's avatar
Robbert Krebbers committed
45 46
Context {Φ_split :  x, Φ x  (Φ1 x  Φ2 x)}.
Context {Ψ_join  :  x, (Ψ1 x  Ψ2 x)  Ψ x}.
Ralf Jung's avatar
Ralf Jung committed
47

48
Lemma P_res_split γ : barrier_res γ Φ  barrier_res γ Φ1  barrier_res γ Φ2.
Ralf Jung's avatar
Ralf Jung committed
49
Proof.
50
  iDestruct 1 as (x) "[#Hγ Hx]".
51
  iDestruct (Φ_split with "Hx") as "[H1 H2]". by iSplitL "H1"; iExists x; iSplit.
Ralf Jung's avatar
Ralf Jung committed
52 53
Qed.

54
Lemma Q_res_join γ : barrier_res γ Ψ1  barrier_res γ Ψ2   barrier_res γ Ψ.
Ralf Jung's avatar
Ralf Jung committed
55
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
56
  iIntros "[Hγ Hγ']";
57
  iDestruct "Hγ" as (x) "[#Hγ Hx]"; iDestruct "Hγ'" as (x') "[#Hγ' Hx']".
58
  iAssert ( (x  x'))%I as "Hxx".
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
59
  { iCombine "Hγ" "Hγ'" as "Hγ2". iClear "Hγ Hγ'".
60
    rewrite own_valid csum_validI /= agree_validI agree_equivI uPred.later_equivI /=.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
61
    rewrite -{2}[x]cFunctor_id -{2}[x']cFunctor_id.
62
    rewrite (ne_proper (cFunctor_map F) (cid, cid) (_  _, _  _)); last first.
Robbert Krebbers's avatar
Robbert Krebbers committed
63
    { by split; intro; simpl; symmetry; apply iProp_fold_unfold. }
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
64
    rewrite !cFunctor_compose. iNext. by iRewrite "Hγ2". }
Robbert Krebbers's avatar
Robbert Krebbers committed
65 66
  iNext. iRewrite -"Hxx" in "Hx'".
  iExists x; iFrame "Hγ". iApply Ψ_join; by iSplitL "Hx".
Ralf Jung's avatar
Ralf Jung committed
67 68
Qed.

69 70
Lemma client_spec_new eM eW1 eW2 `{!Closed [] eM, !Closed [] eW1, !Closed [] eW2} :
  heapN  N 
71
  heap_ctx  P
72 73
   {{ P }} eM {{ _,  x, Φ x }}
   ( x, {{ Φ1 x }} eW1 {{ _, Ψ1 x }})
74
   ( x, {{ Φ2 x }} eW2 {{ _, Ψ2 x }})
75
   WP client eM eW1 eW2 {{ _,  γ, barrier_res γ Ψ }}.
Ralf Jung's avatar
Ralf Jung committed
76
Proof.
77
  iIntros (HN) "/= (#Hh&HP&#He&#He1&#He2)"; rewrite /client.
78
  iVs (own_alloc (Pending : one_shotR Σ F)) as (γ) "Hγ"; first done.
79
  wp_apply (newbarrier_spec N (barrier_res γ Φ)); auto.
80
  iFrame "Hh". iIntros (l) "[Hr Hs]".
Robbert Krebbers's avatar
Robbert Krebbers committed
81
  set (workers_post (v : val) := (barrier_res γ Ψ1  barrier_res γ Ψ2)%I).
82
  wp_let. wp_apply (wp_par  (λ _, True)%I workers_post); iFrame "Hh".
83
  iSplitL "HP Hs Hγ"; [|iSplitL "Hr"].
84
  - wp_bind eM. iApply wp_wand_l; iSplitR "HP"; [|by iApply "He"].
85
    iIntros (v) "HP"; iDestruct "HP" as (x) "HP". wp_let.
86
    iVs (own_update with "Hγ") as "Hx".
87
    { by apply (cmra_update_exclusive (Shot x)). }
Robbert Krebbers's avatar
Robbert Krebbers committed
88
    iApply signal_spec; iFrame "Hs"; iSplit; last done.
89
    iExists x; auto.
90
  - iDestruct (recv_weaken with "[] Hr") as "Hr"; first by iApply P_res_split.
91
    iVs (recv_split with "Hr") as "[H1 H2]"; first done.
92 93
    wp_apply (wp_par (λ _, barrier_res γ Ψ1)%I
                     (λ _, barrier_res γ Ψ2)%I); iFrame "Hh".
94
    iSplitL "H1"; [|iSplitL "H2"].
95 96 97
    + iApply worker_spec; auto.
    + iApply worker_spec; auto.
    + auto.
98
  - iIntros (_ v) "[_ H]"; iPoseProof (Q_res_join with "H"). auto.
Ralf Jung's avatar
Ralf Jung committed
99 100
Qed.
End proof.