CG_stack.v 9.74 KB
Newer Older
1
From iris.proofmode Require Import tactics.
2
From iris_logrel Require Export logrel examples.lock.
3 4
Import uPred.

5 6
(* Stack τ = μ x. Unit + (τ * x), essentially a type of lists *)
(* writing nil and cons for "constructors" *)
7 8 9
Definition CG_StackType τ :=
  TRec (TSum TUnit (TProd τ.[ren (+1)] (TVar 0))).

Dan Frumin's avatar
Dan Frumin committed
10 11
Notation Conse h t := (Fold (SOME (Pair h t))).
Notation Nile := (Fold NONE).
12

13
(* Coarse-grained push *)
14 15
Program Definition CG_push : val := λ: "st" "x",
  "st" <- Conse "x" (!"st").
16

17 18
Definition CG_locked_push : val := λ: "st" "l" "x",
  acquire "l";; CG_push "st" "x";; release "l".
19

20 21 22 23
(* pop s = λ x. match (load s) with
                | nil => InjL ()
                | cons y ys => s <- ys ;; InjR y 
                end *)
24
Definition CG_pop : val := λ: "st" <>,
25
  match: Unfold !"st" with
Dan Frumin's avatar
Dan Frumin committed
26 27
    NONE => NONE
  | SOME "y" => "st" <- (Snd "y");; SOME (Fst "y")
28 29 30 31
  end.


Definition CG_locked_pop : val := λ: "st" "l" <>,
32
  acquire "l";; (let: "v" := CG_pop "st" #() in (release "l";; "v")).
33

34
(* snap st l = with_lock (λ _, load st) l *)
35 36
Definition CG_snap : val := λ: "st" "l" <>,
  acquire "l";; let: "v" := !"st" in (release "l";; "v").
37

38 39 40 41
(* iter f = λ s. match s with
                 | nil => Unit
                 | cons x xs => (f x) ;; iter f xs
                 end *)
42
Definition CG_iter : val := rec: "iter" "f" := λ: "s",
43
  match: (Unfold "s") with
Dan Frumin's avatar
Dan Frumin committed
44 45
    NONE => #()
  | SOME "x" => "f" (Fst "x");; "iter" "f" (Snd "x")
46
  end.
47

48
(* snap_iter st l := λ f. iter f (snap st l #()) *)
49
Definition CG_snap_iter : val := λ: "st" "l" "f",
50
  CG_iter "f" (CG_snap "st" "l" #()).
51 52 53

(* stack_body st l :=
   locked_push st l, locked_pop st l, snap_iter st l *)
54 55
Definition CG_stack_body : val := λ: "st" "l",
  (CG_locked_push "st" "l", CG_locked_pop "st" "l", CG_snap_iter "st" "l").
56

57
Definition CG_stack : val :=
58
  Λ: let: "l" := ref #false in 
59 60
     let: "st" := ref Nile in
     CG_stack_body "st" "l".
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
(** Coarse-grained stack presented as a module *)
(* type s α := (ref (list α), lockτ) *)
Definition sτ α := TProd (Tref (CG_StackType α)) LockType.
(* newStack :  α, s α *)
Definition newStack : val := Λ: λ: <>,
  (ref Nile, ref #false)%E.
(* popStack :  α, s α  MAYBE α *)
Definition popStack : val := Λ: λ: "x",
  CG_locked_pop (Fst "x") (Snd "x") #().
(* pushStack :  α, s α  α  () *)
Definition pushStack : val := Λ: λ: "x" "a",
  CG_locked_push (Fst "x") (Snd "x") "a".
Definition stackmod : val := Λ:
  Pack (TApp newStack, TApp popStack, TApp pushStack).

Section typing.
  Hint Unfold sτ : typeable.
  Lemma newStack_typed Γ :
    Γ ⊢ₜ newStack : TForall (TArrow TUnit (sτ (TVar 0))).
  Proof.
    unlock sτ newStack. (* TODO need to explicitly unlock newStack here *)
    solve_typed.
  Qed.
  Hint Resolve newStack_typed : typeable.

  Lemma popStack_typed Γ :
    Γ ⊢ₜ popStack : TForall $ TArrow (sτ (TVar 0)) (TSum TUnit (TVar 0)).
  Proof.
    unlock sτ popStack. (* TODO need to explicitly unlock newStack here *)
    unlock CG_locked_pop CG_pop.
    repeat (econstructor; solve_typed).
  Qed.
  Hint Resolve popStack_typed : typeable.

  Lemma pushStack_typed Γ :
    Γ ⊢ₜ pushStack : TForall $ TArrow (sτ (TVar 0)) (TArrow (TVar 0) TUnit).
  Proof.
    unlock sτ pushStack. (* TODO need to explicitly unlock newStack here *)
    unlock CG_locked_push CG_push.
    repeat (econstructor; solve_typed).
  Qed.
  Hint Resolve pushStack_typed : typeable.

  Lemma stackmod_typed Γ :
    Γ ⊢ₜ stackmod : TForall $ TExists $ TProd (TProd
                                                 (TArrow TUnit (TVar 0))
                                                 (TArrow (TVar 0) (TSum TUnit (TVar 1))))
                                                 (TArrow (TVar 0) (TArrow (TVar 1) TUnit)).
  Proof.
    unlock stackmod.
    econstructor.
    eapply TPack with (sτ (TVar 0)).
    econstructor; [econstructor | ].
    - simpl.
      replace (TArrow TUnit (sτ (TVar 0))) with (TArrow TUnit (sτ (TVar 0))).[TVar 0/]; last first.
      { autosubst. }
      solve_typed.
    - simpl.
      replace (TArrow (sτ (TVar 0)) (TSum TUnit (ids 0)))
        with (TArrow (sτ (TVar 0)) (TSum TUnit (TVar 0))).[TVar 0/]; last first.
      { autosubst. }
      solve_typed.
    - simpl.
      replace (TArrow (sτ (TVar 0)) (TArrow (ids 0) TUnit))
        with (TArrow (sτ (TVar 0)) (TArrow (ids 0) TUnit)).[TVar 0/] by autosubst.
      solve_typed.
  Qed.
  Hint Resolve stackmod_typed.
End typing.

132
Section CG_Stack.
133
  Context `{logrelG Σ}.
134

135 136
  Lemma CG_push_type Γ τ :
    typed Γ CG_push (TArrow (Tref (CG_StackType τ)) (TArrow τ TUnit)).
137
  Proof.
138 139 140 141
    unfold CG_push. unlock.
    repeat econstructor. eauto 10 with typeable.
    (* TODO: make eauto call asimpl? *)
    asimpl. eauto 10 with typeable.
142 143
  Qed.

144
  Hint Resolve CG_push_type : typeable.
145

146 147
  Lemma CG_locked_push_type Γ τ :
    typed Γ CG_locked_push (TArrow (Tref (CG_StackType τ)) (TArrow LockType (TArrow τ TUnit))).
148
  Proof.
149 150
    unfold CG_locked_push. unlock.
    eauto 20 with typeable.
151
  Qed.
152 153 154
  
  Hint Resolve CG_locked_push_type : typeable.

155 156 157
  Lemma CG_push_r st' (v w : val) l E1 E2 Δ Γ t K τ :
    nclose logrelN  E1 
    st' ↦ₛ v - l ↦ₛ #false -
Dan Frumin's avatar
Dan Frumin committed
158
    (st' ↦ₛ FoldV (SOMEV (w, v)) - l ↦ₛ #false
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
     - {E1,E2;Δ;Γ}  t log fill K #() : τ) -
    {E1,E2;Δ;Γ}  t log fill K ((CG_locked_push $/ (LitV (Loc st')) $/ (LitV (Loc l))) w) : τ.
  Proof.
    iIntros (?)"Hst' Hl Hlog".
    unlock CG_locked_push CG_push. simpl_subst/=.
    rel_let_r.
    rel_apply_r (bin_log_related_acquire_r with "Hl").
    { solve_ndisj. }
    iIntros "Hl /=".
    rel_seq_r.
    do 2 rel_let_r.
    rel_load_r.
    rel_store_r.
    rel_seq_r.
    rel_apply_r (bin_log_related_release_r with "Hl").
    { solve_ndisj. }
    iIntros "Hl /=".
    iApply ("Hlog" with "Hst' Hl").
  Qed.
178

179
  (* Coarse-grained pop *)
180 181
  Lemma CG_pop_type Γ τ :
    typed Γ CG_pop (TArrow (Tref (CG_StackType τ)) (TArrow TUnit (TSum TUnit τ))).
182
  Proof.
183 184 185
    unfold CG_pop. unlock.
    repeat econstructor; eauto 20 with typeable.
    asimpl. eauto 20 with typeable.
186 187
  Qed.

188
  Hint Resolve CG_pop_type : typeable.
189 190
  Global Opaque CG_pop.

191 192
  Lemma CG_locked_pop_type Γ τ :
    typed Γ CG_locked_pop (TArrow (Tref (CG_StackType τ)) (TArrow LockType (TArrow TUnit (TSum TUnit τ)))).
193
  Proof.
194 195
    unfold CG_locked_pop. unlock.
    eauto 20 with typeable.
196 197
  Qed.

198
  Hint Resolve CG_locked_pop_type : typeable.
199 200 201

  Lemma CG_pop_suc_r st' l (w v : val) E1 E2 Δ Γ t K τ :
    nclose logrelN  E1 
Dan Frumin's avatar
Dan Frumin committed
202
    st' ↦ₛ FoldV (SOMEV (w, v)) -
203 204
    l ↦ₛ #false -
    (st' ↦ₛ v - l ↦ₛ #false
Dan Frumin's avatar
Dan Frumin committed
205
    - {E1,E2;Δ;Γ}  t log fill K (SOME w) : τ) -
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    {E1,E2;Δ;Γ}  t log fill K ((CG_locked_pop $/ LitV (Loc st') $/ LitV (Loc l)) #()) : τ.
  Proof.
    iIntros (?) "Hst' Hl Hlog".
    unlock CG_locked_pop CG_pop. simpl_subst/=.
    rel_seq_r.
    rel_apply_r (bin_log_related_acquire_r with "Hl").
    { solve_ndisj. }
    iIntros "Hl /=".
    repeat rel_rec_r.
    rel_load_r.
    rel_fold_r.
    rel_case_r.
    rel_let_r.
    rel_proj_r.
    rel_store_r.
    rel_seq_r.
    rel_proj_r.
    rel_rec_r.
    rel_apply_r (bin_log_related_release_r with "Hl").
    { solve_ndisj. }
    iIntros "Hl /=".
    rel_rec_r.
    iApply ("Hlog" with "Hst' Hl").
  Qed.

  Lemma CG_pop_fail_r st' l E1 E2 Δ Γ t K τ :
    nclose logrelN  E1 
Dan Frumin's avatar
Dan Frumin committed
233
    st' ↦ₛ FoldV NONEV -
234
    l ↦ₛ #false -
Dan Frumin's avatar
Dan Frumin committed
235 236
    (st' ↦ₛ FoldV NONEV - l ↦ₛ #false
    - {E1,E2;Δ;Γ}  t log fill K NONE : τ) -
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    {E1,E2;Δ;Γ}  t log fill K ((CG_locked_pop $/ LitV (Loc st') $/ LitV (Loc l)) #()) : τ.
  Proof.
    iIntros (?) "Hst' Hl Hlog".
    unlock CG_locked_pop CG_pop. simpl_subst/=.
    rel_seq_r.
    rel_apply_r (bin_log_related_acquire_r with "Hl").
    { solve_ndisj. }
    iIntros "Hl /=".
    repeat rel_rec_r.
    rel_load_r.
    rel_fold_r.
    rel_case_r.
    repeat rel_let_r.
    rel_apply_r (bin_log_related_release_r with "Hl").
    { solve_ndisj. }
    iIntros "Hl /=".
    rel_rec_r.
    iApply ("Hlog" with "Hst' Hl").
  Qed.
256 257 258

  Global Opaque CG_locked_pop.

259 260
  Lemma CG_snap_type Γ τ :
    typed Γ CG_snap (TArrow (Tref (CG_StackType τ)) (TArrow LockType (TArrow TUnit (CG_StackType τ)))).
261
  Proof.
262 263
    unfold CG_snap. unlock.
    eauto 20 with typeable.
264 265
  Qed.

266
  Hint Resolve CG_snap_type : typeable.
267 268
  Global Opaque CG_snap.

269 270
  Lemma CG_iter_type Γ τ :
    typed Γ CG_iter (TArrow (TArrow τ TUnit) (TArrow (CG_StackType τ) TUnit)).
271
  Proof.
272 273 274
    unfold CG_iter. unlock.
    repeat econstructor; eauto 50 with typeable.
    asimpl. eauto with typeable.
275 276
  Qed.

277
  Hint Resolve CG_iter_type : typeable.
278 279
  Global Opaque CG_iter.

280 281
  Lemma CG_snap_iter_type Γ τ :
    typed Γ CG_snap_iter (TArrow (Tref (CG_StackType τ)) (TArrow LockType (TArrow (TArrow τ TUnit) TUnit))).
282
  Proof.
283 284
    unfold CG_snap_iter. unlock.
    eauto 50 with typeable.
285 286
  Qed.

287 288 289 290
  Hint Resolve CG_snap_iter_type : typeable.
  
  Lemma CG_stack_body_type Γ τ :
    typed Γ CG_stack_body (TArrow (Tref (CG_StackType τ)) (TArrow LockType
291 292 293
          (TProd
             (TProd (TArrow τ TUnit) (TArrow TUnit (TSum TUnit τ)))
             (TArrow (TArrow τ TUnit) TUnit)
294
          ))).
295
  Proof.
296 297
    unfold CG_stack_body. unlock.
    eauto 50 with typeable.
298 299
  Qed.

300
  Hint Resolve CG_stack_body_type : typeable.
301

302
  Opaque CG_snap_iter.
303

304 305
  (* CG_stack 
    :  α. ((α  Unit) * (Unit  Unit + α) * ((α  Unit)  Unit))  *)
306
  Lemma CG_stack_type Γ :
307
    typed Γ CG_stack
308 309 310 311 312 313 314 315 316
          (TForall
             (TProd
                (TProd
                   (TArrow (TVar 0) TUnit)
                   (TArrow TUnit (TSum TUnit (TVar 0)))
                )
                (TArrow (TArrow (TVar 0) TUnit) TUnit)
          )).
  Proof.
317 318
    unfold CG_stack. unlock.
    eauto 50 with typeable.
319 320
  Qed.

321
End CG_Stack.