sync.v 8.54 KB
Newer Older
Zhen Zhang's avatar
Zhen Zhang committed
1
2
3
4
5
6
From iris.program_logic Require Export weakestpre hoare.
From iris.proofmode Require Import invariants ghost_ownership.
From iris.heap_lang Require Export lang.
From iris.heap_lang Require Import proofmode notation.
From iris.heap_lang.lib Require Import spin_lock.
From iris.tests Require Import atomic.
Zhen Zhang's avatar
srv    
Zhen Zhang committed
7
8
From iris.algebra Require Import dec_agree frac.
From iris.program_logic Require Import auth.
Zhen Zhang's avatar
Zhen Zhang committed
9
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
42
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
Import uPred.

(* See CaReSL paper §3.2 *)
Definition mk_sync: val :=
  λ: <>,
       let: "l" := newlock #() in
       λ: "f" "x",
          acquire "l";;
          let: "ret" := "f" "x" in
          release "l";;
          "ret".

Global Opaque mk_sync.

Section proof.
  Context `{!heapG Σ, !lockG Σ} (N: namespace).

  (* concurrent object triple: R, p . <l, α p l> e <v, β p l v>*)
  Definition conc_obj_triple {A: Type}
             (α: val  A  iProp Σ)
             (β: val  A  val  iProp Σ)
             (Ei Eo: coPset)
             (e: expr) : iProp Σ :=
    ( P Q,
       ( ( (R: val  iProp Σ) p,
             P  R p -
                l: A, α p l 
                 ( v, β p l v - Q l v  R p)))
          - {{ P }} e {{ v,  l, Q l v }})%I.

  Arguments conc_obj_triple {_} _ _ _ _ _.
  
  (* f' refines f *)
  Definition refines (f' f: val) (R: iProp Σ): iProp Σ :=
    (  (x: expr) (v: val) (P: iProp Σ) (Q: val -> iProp Σ),
         to_val x = Some v -
         {{ R  P }} f x {{ z, R  Q z }} -
         {{ P }} f' x {{ z, Q z }})%I.

  Global Instance refines_persistent f f' R: PersistentP (refines f f' R).
  Proof. apply _. Qed.

  Global Opaque refines.
  
  Definition is_syncer (R: iProp Σ) (s: val) : iProp Σ :=
    ( (f : val), WP s f {{ f', refines f' f R }})%I.

  Lemma mk_sync_spec_wp:
     (R: iProp Σ) (Φ: val -> iProp Σ),
      heapN  N 
      heap_ctx  R  ( s,  (is_syncer R s) - Φ s)  WP mk_sync #() {{ Φ }}.
  Proof.
    iIntros (R Φ HN) "(#Hh & HR & HΦ)".
    wp_seq. wp_bind (newlock _).
    iApply newlock_spec; first by auto.
    iSplitR "HR HΦ"; first by auto.
    iFrame "HR".
    iIntros (lk γ) "#Hl". wp_let. iApply "HΦ". iIntros "!#".
    rewrite /is_syncer. iIntros (f).
    wp_let. iVsIntro. rewrite /refines. iIntros "!#".
    iIntros (x v P Q <-%of_to_val) "#Hf !# HP".
    wp_let.
    wp_bind (acquire _).
    iApply acquire_spec.
    iSplit; first by auto.
    iIntros "Hlocked R".
    wp_seq. wp_bind (f _).
    iDestruct ("Hf" with "[R HP]") as "Hf'"; first by iFrame.
    iApply wp_wand_r.
    iSplitL "Hf'"; first by auto.
    iIntros (v') "[HR HQv]".
    wp_let.
    wp_bind (release _).
    iApply release_spec.
    iFrame "HR".
    iSplitR; eauto.
    iFrame.
    by wp_seq.
  Qed.
Zhen Zhang's avatar
srv    
Zhen Zhang committed
88
End proof.
Zhen Zhang's avatar
Zhen Zhang committed
89

Zhen Zhang's avatar
Zhen Zhang committed
90
91
92
Definition syncR := authR (optionUR (prodR fracR (dec_agreeR val))).  (* FIXME: can't use A instead of val *)
Class syncG Σ := sync_tokG :> inG Σ syncR.
Definition syncΣ : gFunctors := #[GFunctor (constRF syncR)].
Zhen Zhang's avatar
Zhen Zhang committed
93

Zhen Zhang's avatar
Zhen Zhang committed
94
95
Instance subG_syncΣ {Σ} : subG syncΣ Σ  syncG Σ.
Proof. by intros ?%subG_inG. Qed.
Zhen Zhang's avatar
Zhen Zhang committed
96

Zhen Zhang's avatar
Zhen Zhang committed
97
98
Section generic.
  Context `{!heapG Σ, !lockG Σ, !syncG Σ} (N : namespace).
Zhen Zhang's avatar
Zhen Zhang committed
99

Zhen Zhang's avatar
Zhen Zhang committed
100
  Definition A := val.
Zhen Zhang's avatar
Zhen Zhang committed
101

Zhen Zhang's avatar
Zhen Zhang committed
102
103
104
105
106
107
108
109
  Definition gFragR g : syncR :=  Some ((1/2)%Qp, DecAgree g).
  Definition gFullR g : syncR :=  Some ((1/2)%Qp, DecAgree g).

  Definition gFrag (γ: gname) g : iProp Σ := own γ (gFragR g).
  Definition gFull (γ: gname) g : iProp Σ := own γ (gFullR g).
  
  Global Instance frag_timeless γ g: TimelessP (@own Σ syncR sync_tokG γ (gFragR g)).
  Proof. apply _. Qed.
Zhen Zhang's avatar
Zhen Zhang committed
110

Zhen Zhang's avatar
Zhen Zhang committed
111
112
  Global Instance full_timeless γ g: TimelessP (gFull γ g).
  Proof. apply _. Qed.
Zhen Zhang's avatar
srv    
Zhen Zhang committed
113
  
Zhen Zhang's avatar
Zhen Zhang committed
114
115
116
117
118
119
120
121
  Definition atomic_triple'
             (α: val  iProp Σ)
             (β: val  A  A  val  iProp Σ)
             (Ei Eo: coPset)
             (f x: val) γ : iProp Σ :=
    ( P Q, ( g, (P ={Eo, Ei}=> gFrag γ g  α x) 
                  ( g' r, gFrag γ g'  β x g g' r ={Ei, Eo}=> Q r))
            - {{ P }} f x {{ Q }})%I.
Zhen Zhang's avatar
polish    
Zhen Zhang committed
122

123

Zhen Zhang's avatar
Zhen Zhang committed
124
125
  Definition sync : val :=
    λ: "f_cons" "f_seq",
126
127
128
129
        let: "l"  := "f_cons" #() in
        let: "s" := mk_sync #() in
        "s" ("f_seq" "l").

Zhen Zhang's avatar
Zhen Zhang committed
130
  Definition seq_spec (f: val) (ϕ: val  A  iProp Σ) α β (E: coPset) :=
131
132
133
       (Φ: val  iProp Σ) (l: val),
         {{ True }} f l {{ f',  ( (x: val) (Φ: val  iProp Σ) (g: A),
                               heapN  N 
Zhen Zhang's avatar
Zhen Zhang committed
134
135
136
                               heap_ctx  ϕ l g  α x 
                               ( (v: val) (g': A), ϕ l g' - β x g g' v - |={E}=> Φ v)
                                WP f' x @ E {{ Φ }} )}}.
Zhen Zhang's avatar
Zhen Zhang committed
137
    
Zhen Zhang's avatar
Zhen Zhang committed
138
139
140
  Definition cons_spec (f: val) (g: A) ϕ :=
       Φ: val  iProp Σ, heapN  N 
        heap_ctx  ( (l: val) (γ: gname), ϕ l g - gFull γ g - gFrag γ g - Φ l)
141
         WP f #() {{ Φ }}.
Zhen Zhang's avatar
Zhen Zhang committed
142
    
Zhen Zhang's avatar
Zhen Zhang committed
143
  Lemma atomic_spec (f_cons f_seq: val) (ϕ: val  A  iProp Σ) α β:
144
       (g0: A),
Zhen Zhang's avatar
Zhen Zhang committed
145
        heapN  N  seq_spec f_seq ϕ α β   cons_spec f_cons g0 ϕ 
146
147
        heap_ctx
         WP sync f_cons f_seq {{ f,  γ, gFrag γ g0   x,  atomic_triple' α β   f x γ  }}.
Zhen Zhang's avatar
Zhen Zhang committed
148
  Proof.
Zhen Zhang's avatar
Zhen Zhang committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    iIntros (g0 HN Hseq Hcons) "#Hh". repeat wp_let.
    wp_bind (f_cons _). iApply Hcons=>//. iFrame "Hh".
    iIntros (l γ) "Hϕ HFull HFrag".
    wp_let. wp_bind (mk_sync _).
    iApply mk_sync_spec_wp=>//.
    iAssert ( g: A, ϕ l g  gFull γ g)%I with "[-HFrag]" as "HR".
    { iExists g0. by iFrame. }
    iFrame "Hh HR".
    iIntros (s) "#Hsyncer".
    wp_let. rewrite /is_syncer /seq_spec.
    wp_bind (f_seq _). iApply wp_wand_r. iSplitR; first by iApply (Hseq with "[]")=>//.
    iIntros (f) "%". (* FIXME: name *)
    iApply wp_wand_r. iSplitR; first by iApply "Hsyncer".
    iIntros (v) "#Hrefines".
    iExists γ. iFrame. iIntros (x).
    iAlways. rewrite /atomic_triple'.
    iIntros (P Q) "#Hvss".
    rewrite /refines.
    iDestruct "Hrefines" as "#Hrefines".
    iSpecialize ("Hrefines" $! (of_val x) x P Q).
    iApply ("Hrefines" with "[]"); first by rewrite to_of_val.
    iAlways. iIntros "[HR HP]". iDestruct "HR" as (g) "[Hϕ HgFull]".
    (* we should view shift at this point *)
    iDestruct ("Hvss" $! g) as "[Hvs1 Hvs2]".
    iVs ("Hvs1" with "HP") as "[HgFrag Hα]".
    iApply pvs_wp. iVsIntro.
    iApply H=>//. (* FIXME: name *)
    iFrame "Hh Hϕ Hα". iIntros (ret g') "Hϕ' Hβ".
    iCombine "HgFull" "HgFrag" as "Hg".
    iVs (own_update with "Hg") as "[HgFull HgFrag]".
179
    { admit . }
Zhen Zhang's avatar
Zhen Zhang committed
180
181
182
    iSplitL "HgFull Hϕ'".
    - iExists g'. by iFrame.
    - by iVs ("Hvs2" $! g' ret with "[HgFrag Hβ]"); first by iFrame.
183
  Admitted.
Zhen Zhang's avatar
Zhen Zhang committed
184
185
  
End generic.
Zhen Zhang's avatar
Zhen Zhang committed
186

Zhen Zhang's avatar
Zhen Zhang committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Section sync_atomic.
  Context `{!heapG Σ, !lockG Σ} (N : namespace) {A: Type}.

  Variable α: val  A  iProp Σ.
  Variable β: val  A  val  iProp Σ.
  Variable f_cons f_seq: val.
  Variable R: val  iProp Σ.

  Definition mk_whatever (f_cons: val) (f_seq: val) : val :=
    λ: <>,
         let: "x" := f_cons #() in
         let: "s" := mk_sync #() in
         "s" (λ:<>, f_seq "x").

  Definition whatever_triple (obj: val) :=
    conc_obj_triple α β (nclose heapN)  (obj #()).

  Definition whatever_seq_spec :=
     (p: val) (l: A) (Φ: val  iProp Σ),
      heapN  N 
      heap_ctx  α p l  ( v, β p l v - Φ v)
       WP f_seq p {{ Φ }}.

  Definition f_cons_spec :=
     (Φ: val  iProp Σ),
      heapN  N 
      heap_ctx  ( v, R v - Φ v)%I
       WP f_cons #() {{ Φ }}.
  
  Lemma mk_whatever_spec:
     (Φ: val  iProp Σ),
      heapN  N 
      whatever_seq_spec 
      f_cons_spec 
Zhen Zhang's avatar
Zhen Zhang committed
221
      heap_ctx 
Zhen Zhang's avatar
Zhen Zhang committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
      ( obj, whatever_triple obj - Φ obj)
       WP mk_whatever f_cons f_seq #() {{ Φ }}.
  Proof.
    iIntros (Φ HN Hseq Hcons) "[#Hh HΦ]".
    wp_let. wp_bind (f_cons _). iApply Hcons=>//.
    iFrame "Hh".
    iIntros (v) "HR".
    wp_let. rewrite /mk_sync. (* TODO: use the derived lemmas above *)
    wp_seq. wp_bind (newlock _). iApply (newlock_spec)=>//.
    iFrame "Hh HR".
    iIntros (lk γ) "#Hlk".
    repeat wp_let.
    iApply "HΦ".
    rewrite /whatever_triple /conc_obj_triple.
    iIntros (P Q) "#H".
    iAlways. iIntros "HP". wp_seq.
    iSpecialize ("H" $! R v).
    wp_bind (acquire _).
    iApply acquire_spec.
    iFrame "Hlk". iIntros "Hlked HR".
    iDestruct ("H" with "[HP HR]") as (x) "[Hl Hnext]"; first by iFrame.
    wp_seq. wp_let.
    iApply Hseq=>//. iFrame "Hh Hl".
    iIntros (v') "Hbeta".
    iDestruct ("Hnext" $! v' with "Hbeta") as "[HQ HR]".
    wp_let. wp_bind (release _). iApply release_spec.
    iFrame "Hlk Hlked HR".
    wp_seq. iVsIntro. by iExists x.
  Qed.
End sync_atomic.