Robbert Krebbers committed Jan 08, 2018 1 2 3 4 5 6 7 8 ``````(** In this exercise we use the spin-lock from the previous exercise to implement the running example during the lecture of the tutorial: proving that when two threads increase a reference that's initially zero by two, the result is four. *) From iris.algebra Require Import auth frac_auth. From iris.base_logic.lib Require Import invariants. From iris.heap_lang Require Import lib.par proofmode notation. `````` Ralf Jung committed Feb 01, 2019 9 ``````From exercises Require Import ex_03_spinlock. `````` Robbert Krebbers committed Jan 08, 2018 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 `````` (** The program as a heap-lang expression. We use the heap-lang [par] module for parallel composition. *) Definition parallel_add : expr := let: "r" := ref #0 in let: "l" := newlock #() in ( (acquire "l";; "r" <- !"r" + #2;; release "l") ||| (acquire "l";; "r" <- !"r" + #2;; release "l") );; acquire "l";; !"r". (** 1st proof : we only prove that the return value is even. No ghost state is involved in this proof. *) Section proof1. Context `{!heapG Σ, !spawnG Σ}. Definition parallel_add_inv_1 (r : loc) : iProp Σ := (∃ n : Z, r ↦ #n ∗ ⌜ Zeven n ⌝)%I. (** *Exercise*: finish the missing cases of this proof. *) Lemma parallel_add_spec_1 : {{{ True }}} parallel_add {{{ n, RET #n; ⌜Zeven n⌝ }}}. Proof. iIntros (Φ) "_ Post". unfold parallel_add. wp_alloc r as "Hr". wp_let. wp_apply (newlock_spec (parallel_add_inv_1 r) with "[Hr]"). { (* exercise *) admit. } iIntros (l) "#Hl". wp_let. `````` Rodolphe Lepigre committed Feb 12, 2019 42 `````` wp_apply (par_spec (λ _, True%I) (λ _, True%I)). `````` Robbert Krebbers committed Jan 08, 2018 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 `````` - wp_apply (acquire_spec with "Hl"). iDestruct 1 as (n) "[Hr %]". wp_seq. wp_load. wp_op. wp_store. wp_apply (release_spec with "[Hr \$Hl]"); [|done]. iExists _. iFrame "Hr". iPureIntro. by apply Zeven_plus_Zeven. - (* exercise *) admit. - (* exercise *) admit. Admitted. End proof1. (** 2nd proof : we prove that the program returns 4 exactly. We need a piece of ghost state: integer ghost variables. Whereas we previously abstracted over an arbitrary "ghost state" [Σ] in the proofs, we now need to make sure that we can use integer ghost variables. For this, we add the type class constraint: inG Σ (authR (optionUR (exclR ZC))) *) Section proof2. Context `{!heapG Σ, !spawnG Σ, !inG Σ (authR (optionUR (exclR ZC)))}. Definition parallel_add_inv_2 (r : loc) (γ1 γ2 : gname) : iProp Σ := (∃ n1 n2 : Z, r ↦ #(n1 + n2) ∗ own γ1 (● (Excl' n1)) ∗ own γ2 (● (Excl' n2)))%I. (** Some helping lemmas for ghost state that we need in the proof. In actual proofs we tend to inline simple lemmas like these, but they are here to make things easier to understand. *) Lemma ghost_var_alloc n : (|==> ∃ γ, own γ (● (Excl' n)) ∗ own γ (◯ (Excl' n)))%I. Proof. iMod (own_alloc (● (Excl' n) ⋅ ◯ (Excl' n))) as (γ) "[??]"; by eauto with iFrame. Qed. Lemma ghost_var_agree γ n m : own γ (● (Excl' n)) -∗ own γ (◯ (Excl' m)) -∗ ⌜ n = m ⌝. Proof. iIntros "Hγ● Hγ◯". by iDestruct (own_valid_2 with "Hγ● Hγ◯") as %[<-%Excl_included%leibniz_equiv _]%auth_valid_discrete_2. Qed. Lemma ghost_var_update γ n' n m : own γ (● (Excl' n)) -∗ own γ (◯ (Excl' m)) ==∗ own γ (● (Excl' n')) ∗ own γ (◯ (Excl' n')). Proof. iIntros "Hγ● Hγ◯". iMod (own_update_2 _ _ _ (● Excl' n' ⋅ ◯ Excl' n') with "Hγ● Hγ◯") as "[\$\$]". { by apply auth_update, option_local_update, exclusive_local_update. } done. Qed. (** *Exercise*: finish the missing cases of the proof. *) Lemma parallel_add_spec_2 : {{{ True }}} parallel_add {{{ RET #4; True }}}. Proof. iIntros (Φ) "_ Post". unfold parallel_add. wp_alloc r as "Hr". wp_let. iMod (ghost_var_alloc 0) as (γ1) "[Hγ1● Hγ1◯]". iMod (ghost_var_alloc 0) as (γ2) "[Hγ2● Hγ2◯]". wp_apply (newlock_spec (parallel_add_inv_2 r γ1 γ2) with "[Hr Hγ1● Hγ2●]"). { (* exercise *) admit. } iIntros (l) "#Hl". wp_let. `````` Rodolphe Lepigre committed Feb 12, 2019 111 `````` wp_apply (par_spec (λ _, own γ1 (◯ Excl' 2)) (λ _, own γ2 (◯ Excl' 2)) `````` Robbert Krebbers committed Jan 08, 2018 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 `````` with "[Hγ1◯] [Hγ2◯]"). - wp_apply (acquire_spec with "Hl"). iDestruct 1 as (n1 n2) "(Hr & Hγ1● & Hγ2●)". wp_seq. wp_load. wp_op. wp_store. iDestruct (ghost_var_agree with "Hγ1● Hγ1◯") as %->. iMod (ghost_var_update γ1 2 with "Hγ1● Hγ1◯") as "[Hγ1● Hγ1◯]". wp_apply (release_spec with "[- \$Hl Hγ1◯]"); [|by auto]. iExists _, _. iFrame "Hγ1● Hγ2●". rewrite (_ : 2 + n2 = 0 + n2 + 2); [done|ring]. - (* exercise *) admit. - (* exercise *) admit. Admitted. End proof2. (** 3rd proof : we prove that the program returns 4 exactly, but using a fractional authoritative ghost state. We need another piece of ghost state. *) Section proof3. Context `{!heapG Σ, !spawnG Σ, !inG Σ (frac_authR natR)}. Definition parallel_add_inv_3 (r : loc) (γ : gname) : iProp Σ := (∃ n : nat, r ↦ #n ∗ own γ (●! n))%I. (** *Exercise*: finish the missing cases of the proof. *) Lemma parallel_add_spec_3 : {{{ True }}} parallel_add {{{ RET #4; True }}}. Proof. iIntros (Φ) "_ Post". unfold parallel_add. wp_alloc r as "Hr". wp_let. iMod (own_alloc (●! 0%nat ⋅ ◯! 0%nat)) as (γ) "[Hγ● [Hγ1◯ Hγ2◯]]"; [done|]. wp_apply (newlock_spec (parallel_add_inv_3 r γ) with "[Hr Hγ●]"). { (* exercise *) admit. } iIntros (l) "#Hl". wp_let. `````` Rodolphe Lepigre committed Feb 12, 2019 145 `````` wp_apply (par_spec (λ _, own γ (◯!{1/2} 2%nat)) (λ _, own γ (◯!{1/2} 2%nat)) `````` Robbert Krebbers committed Jan 08, 2018 146 147 148 149 150 151 152 153 154 155 156 157 158 159 `````` with "[Hγ1◯] [Hγ2◯]"). - wp_apply (acquire_spec with "Hl"). iDestruct 1 as (n) "[Hr Hγ●]". wp_seq. wp_load. wp_op. wp_store. iMod (own_update_2 _ _ _ (●! (n+2) ⋅ ◯!{1/2}2)%nat with "Hγ● Hγ1◯") as "[Hγ● Hγ1◯]". { rewrite (comm plus). by apply frac_auth_update, (op_local_update_discrete n 0 2)%nat. } wp_apply (release_spec with "[\$Hl Hr Hγ●]"); [|by auto]. iExists _. iFrame. by rewrite Nat2Z.inj_add. - (* exercise *) admit. - (* exercise *) admit. Admitted. End proof3.``````