spawn.v 3.02 KB
Newer Older
1
From iris.program_logic Require Export weakestpre.
2
From iris.base_logic.lib Require Export invariants.
3
From iris.heap_lang Require Export lang.
4
From iris.proofmode Require Import tactics.
Robbert Krebbers's avatar
Robbert Krebbers committed
5
From iris.heap_lang Require Import proofmode notation.
6
From iris.algebra Require Import excl.
Ralf Jung's avatar
Ralf Jung committed
7 8

Definition spawn : val :=
9
  λ: "f",
Robbert Krebbers's avatar
Robbert Krebbers committed
10
    let: "c" := ref NONE in
11
    Fork ("c" <- SOME ("f" #())) ;; "c".
Ralf Jung's avatar
Ralf Jung committed
12
Definition join : val :=
13
  rec: "join" "c" :=
14
    match: !"c" with
Robbert Krebbers's avatar
Robbert Krebbers committed
15 16
      SOME "x" => "x"
    | NONE => "join" "c"
17
    end.
Ralf Jung's avatar
Ralf Jung committed
18

19
(** The CMRA & functor we need. *)
Ralf Jung's avatar
Ralf Jung committed
20
(* Not bundling heapG, as it may be shared with other users. *)
21
Class spawnG Σ := SpawnG { spawn_tokG :> inG Σ (exclR unitC) }.
22
Definition spawnΣ : gFunctors := #[GFunctor (constRF (exclR unitC))].
23

24 25
Instance subG_spawnΣ {Σ} : subG spawnΣ Σ  spawnG Σ.
Proof. intros [?%subG_inG _]%subG_inv. split; apply _. Qed.
Ralf Jung's avatar
Ralf Jung committed
26 27 28

(** Now we come to the Iris part of the proof. *)
Section proof.
29
Context `{!heapG Σ, !spawnG Σ} (N : namespace).
Ralf Jung's avatar
Ralf Jung committed
30

31
Definition spawn_inv (γ : gname) (l : loc) (Ψ : val  iProp Σ) : iProp Σ :=
Ralf Jung's avatar
Ralf Jung committed
32 33
  ( lv, l  lv  (lv = NONEV 
                    v, lv = SOMEV v  (Ψ v  own γ (Excl ()))))%I.
Ralf Jung's avatar
Ralf Jung committed
34

35
Definition join_handle (l : loc) (Ψ : val  iProp Σ) : iProp Σ :=
36
  ( γ, own γ (Excl ())  inv N (spawn_inv γ l Ψ))%I.
Ralf Jung's avatar
Ralf Jung committed
37

Robbert Krebbers's avatar
Robbert Krebbers committed
38 39
Typeclasses Opaque join_handle.

Ralf Jung's avatar
Ralf Jung committed
40 41 42 43 44 45 46 47
Global Instance spawn_inv_ne n γ l :
  Proper (pointwise_relation val (dist n) ==> dist n) (spawn_inv γ l).
Proof. solve_proper. Qed.
Global Instance join_handle_ne n l :
  Proper (pointwise_relation val (dist n) ==> dist n) (join_handle l).
Proof. solve_proper. Qed.

(** The main proofs. *)
48
Lemma spawn_spec (Ψ : val  iProp Σ) e (f : val) :
Ralf Jung's avatar
Ralf Jung committed
49
  to_val e = Some f 
50
  {{{ WP f #() {{ Ψ }} }}} spawn e {{{ l, RET #l; join_handle l Ψ }}}.
Ralf Jung's avatar
Ralf Jung committed
51
Proof.
52
  iIntros (<-%of_to_val Φ) "Hf HΦ". rewrite /spawn /=.
53
  wp_let. wp_alloc l as "Hl". wp_let.
54 55
  iMod (own_alloc (Excl ())) as (γ) "Hγ"; first done.
  iMod (inv_alloc N _ (spawn_inv γ l Ψ) with "[Hl]") as "#?".
Robbert Krebbers's avatar
Robbert Krebbers committed
56
  { iNext. iExists NONEV. iFrame; eauto. }
57
  wp_apply wp_fork; simpl. iSplitR "Hf".
58
  - wp_seq. iApply "HΦ". rewrite /join_handle. eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
59
  - wp_bind (f _). iApply (wp_wand with "Hf"); iIntros (v) "Hv".
60 61
    iInv N as (v') "[Hl _]" "Hclose".
    wp_store. iApply "Hclose". iNext. iExists (SOMEV v). iFrame. eauto.
Ralf Jung's avatar
Ralf Jung committed
62 63
Qed.

64
Lemma join_spec (Ψ : val  iProp Σ) l :
65
  {{{ join_handle l Ψ }}} join #l {{{ v, RET v; Ψ v }}}.
Ralf Jung's avatar
Ralf Jung committed
66
Proof.
67
  rewrite /join_handle; iIntros (Φ) "H HΦ". iDestruct "H" as (γ) "[Hγ #?]".
68
  iLöb as "IH". wp_rec. wp_bind (! _)%E. iInv N as (v) "[Hl Hinv]" "Hclose".
Robbert Krebbers's avatar
Robbert Krebbers committed
69
  wp_load. iDestruct "Hinv" as "[%|Hinv]"; subst.
70
  - iMod ("Hclose" with "[Hl]"); [iNext; iExists _; iFrame; eauto|].
Ralf Jung's avatar
Ralf Jung committed
71
    iModIntro. wp_match. iApply ("IH" with "Hγ [HΦ]"). auto.
72
  - iDestruct "Hinv" as (v') "[% [HΨ|Hγ']]"; simplify_eq/=.
73
    + iMod ("Hclose" with "[Hl Hγ]"); [iNext; iExists _; iFrame; eauto|].
Ralf Jung's avatar
Ralf Jung committed
74
      iModIntro. wp_match. by iApply "HΦ".
75
    + iDestruct (own_valid_2 with "Hγ Hγ'") as %[].
Ralf Jung's avatar
Ralf Jung committed
76
Qed.
Ralf Jung's avatar
Ralf Jung committed
77
End proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
78 79

Typeclasses Opaque join_handle.