barrier_client.v 2.34 KB
Newer Older
1
2
From iris.program_logic Require Export weakestpre.
From iris.heap_lang Require Export lang.
Ralf Jung's avatar
Ralf Jung committed
3
From iris.heap_lang.lib.barrier Require Import proof.
4
From iris.heap_lang Require Import par.
5
From iris.heap_lang Require Import adequacy proofmode.
Ralf Jung's avatar
Ralf Jung committed
6

7
Definition worker (n : Z) : val :=
8
9
  λ: "b" "y", wait "b" ;; !"y" #n.
Definition client : expr :=
10
  let: "y" := ref #0 in
11
  let: "b" := newbarrier #() in
12
13
  ("y" <- (λ: "z", "z" + #42) ;; signal "b") |||
    (worker 12 "b" "y" ||| worker 17 "b" "y").
Ralf Jung's avatar
Ralf Jung committed
14
15

Section client.
16
  Context `{!heapG Σ, !barrierG Σ, !spawnG Σ} (N : namespace).
Ralf Jung's avatar
Ralf Jung committed
17

18
  Definition y_inv (q : Qp) (l : loc) : iProp Σ :=
Ralf Jung's avatar
Ralf Jung committed
19
    ( f : val, l {q} f    n : Z, WP f #n {{ v, v = #(n + 42) }})%I.
20

21
  Lemma y_inv_split q l : y_inv q l - (y_inv (q/2) l  y_inv (q/2) l).
Ralf Jung's avatar
Ralf Jung committed
22
  Proof.
23
    iDestruct 1 as (f) "[[Hl1 Hl2] #Hf]".
Robbert Krebbers's avatar
Robbert Krebbers committed
24
    iSplitL "Hl1"; iExists f; by iSplitL; try iAlways.
Ralf Jung's avatar
Ralf Jung committed
25
26
27
  Qed.

  Lemma worker_safe q (n : Z) (b y : loc) :
28
    recv N b (y_inv q y) - WP worker n #b #y {{ _, True }}.
Ralf Jung's avatar
Ralf Jung committed
29
  Proof.
30
    iIntros "Hrecv". wp_lam. wp_let.
31
    wp_apply (wait_spec with "Hrecv"). iDestruct 1 as (f) "[Hy #Hf]".
Robbert Krebbers's avatar
Robbert Krebbers committed
32
    wp_seq. wp_load.
Robbert Krebbers's avatar
Robbert Krebbers committed
33
    iApply (wp_wand with "[]"). iApply "Hf". by iIntros (v) "_".
Ralf Jung's avatar
Ralf Jung committed
34
35
  Qed.

36
  Lemma client_safe : WP client {{ _, True }}%I.
Ralf Jung's avatar
Ralf Jung committed
37
  Proof.
38
39
    iIntros ""; rewrite /client. wp_alloc y as "Hy". wp_let.
    wp_apply (newbarrier_spec N (y_inv 1 y)).
40
    iIntros (l) "[Hr Hs]". wp_let.
41
    iApply (wp_par (λ _, True%I) (λ _, True%I) with "[Hy Hs] [Hr]"); last auto.
42
    - (* The original thread, the sender. *)
Ralf Jung's avatar
Ralf Jung committed
43
44
      wp_store. iApply (signal_spec with "[-]"); last by iNext; auto.
      iSplitR "Hy"; first by eauto.
45
      iExists _; iSplitL; [done|]. iAlways; iIntros (n). wp_let. by wp_op.
46
    - (* The two spawned threads, the waiters. *)
47
48
      iDestruct (recv_weaken with "[] Hr") as "Hr".
      { iIntros "Hy". by iApply (y_inv_split with "Hy"). }
49
      iMod (recv_split with "Hr") as "[H1 H2]"; first done.
50
51
52
      iApply (wp_par (λ _, True%I) (λ _, True%I) with "[H1] [H2]"); last auto.
      + by iApply worker_safe.
      + by iApply worker_safe.
53
Qed.
Ralf Jung's avatar
Ralf Jung committed
54
End client.
55

56
57
Section ClosedProofs.

58
Let Σ : gFunctors := #[ heapΣ ; barrierΣ ; spawnΣ ].
59

60
61
Lemma client_adequate σ : adequate client σ (λ _, True).
Proof.
62
  apply (heap_adequacy Σ)=> ?. apply (client_safe (nroot .@ "barrier")).
63
Qed.
64

65
End ClosedProofs.
66
67

Print Assumptions client_adequate.