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

Definition spawn : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
8
  λ: "f",
Robbert Krebbers's avatar
Robbert Krebbers committed
9 10
    let: "c" := ref NONE in
    Fork ("c" <- SOME ("f" #())) ;; "c".
Ralf Jung's avatar
Ralf Jung committed
11
Definition join : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
12
  rec: "join" "c" :=
13
    match: !"c" with
Robbert Krebbers's avatar
Robbert Krebbers committed
14 15
      SOME "x" => "x"
    | NONE => "join" "c"
Robbert Krebbers's avatar
Robbert Krebbers committed
16
    end.
17
Global Opaque spawn join.
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 Σ :=
Robbert Krebbers's avatar
Robbert Krebbers 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 37
  (heapN  N   γ, heap_ctx  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) (Φ : val  iProp Σ) :
Ralf Jung's avatar
Ralf Jung committed
50
  to_val e = Some f 
51
  heapN  N 
52
  heap_ctx  WP f #() {{ Ψ }}  ( l, join_handle l Ψ - Φ #l)
53
   WP spawn e {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
54
Proof.
55
  iIntros (<-%of_to_val ?) "(#Hh & Hf & HΦ)". rewrite /spawn /=.
56
  wp_let. wp_alloc l as "Hl". wp_let.
57 58
  iVs (own_alloc (Excl ())) as (γ) "Hγ"; first done.
  iVs (inv_alloc N _ (spawn_inv γ l Ψ) with "[Hl]") as "#?".
Robbert Krebbers's avatar
Robbert Krebbers committed
59
  { iNext. iExists NONEV. iFrame; eauto. }
60
  wp_apply wp_fork; simpl. iSplitR "Hf".
61
  - iVsIntro. wp_seq. iVsIntro. iApply "HΦ". rewrite /join_handle. eauto.
62
  - wp_bind (f _). iApply wp_wand_l. iFrame "Hf"; iIntros (v) "Hv".
63 64
    iInv N as (v') "[Hl _]" "Hclose".
    wp_store. iApply "Hclose". iNext. iExists (SOMEV v). iFrame. eauto.
Ralf Jung's avatar
Ralf Jung committed
65 66
Qed.

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

Typeclasses Opaque join_handle.