spawn.v 3.05 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.
7
Set Default Proof Using "Type*".
Ralf Jung's avatar
Ralf Jung committed
8 9

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

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

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

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

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

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

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

Ralf Jung's avatar
Ralf Jung committed
41 42 43 44 45 46 47 48
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. *)
49
Lemma spawn_spec (Ψ : val  iProp Σ) e (f : val) :
Ralf Jung's avatar
Ralf Jung committed
50
  to_val e = Some f 
51
  {{{ WP f #() {{ Ψ }} }}} spawn e {{{ l, RET #l; join_handle l Ψ }}}.
Ralf Jung's avatar
Ralf Jung committed
52
Proof.
53
  iIntros (<-%of_to_val Φ) "Hf HΦ". rewrite /spawn /=.
54
  wp_let. wp_alloc l as "Hl". wp_let.
55 56
  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
57
  { iNext. iExists NONEV. iFrame; eauto. }
58
  wp_apply wp_fork; simpl. iSplitR "Hf".
59
  - wp_seq. iApply "HΦ". rewrite /join_handle. eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
60
  - wp_bind (f _). iApply (wp_wand with "Hf"); iIntros (v) "Hv".
61 62
    iInv N as (v') "[Hl _]" "Hclose".
    wp_store. iApply "Hclose". iNext. iExists (SOMEV v). iFrame. eauto.
Ralf Jung's avatar
Ralf Jung committed
63 64
Qed.

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

Typeclasses Opaque join_handle.