joining_existentials.v 4.15 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
From iris.heap_lang Require Import notation par proofmode.
6
From iris.proofmode Require Import tactics.
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).
16 17 18 19
Definition oneShotΣ (F : cFunctor) : gFunctors :=
  #[ GFunctor (csumRF (exclRF unitC) (agreeRF ( F))) ].
Instance subG_oneShotΣ {Σ F} : subG (oneShotΣ F) Σ  oneShotG Σ F.
Proof. apply subG_inG. Qed.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
20

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)).
Ralf Jung's avatar
Ralf Jung committed
24 25

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

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

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

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

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

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

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