na_invariants.v 3.94 KB
Newer Older
1
From iris.base_logic.lib Require Export invariants.
2
From iris.algebra Require Export gmap gset coPset.
3
From iris.proofmode Require Import tactics.
4
Set Default Proof Using "Type".
5
6
Import uPred.

7
8
(* Non-atomic ("thread-local") invariants. *)

Ralf Jung's avatar
Ralf Jung committed
9
Definition na_inv_pool_name := gname.
10

11
Class na_invG Σ :=
12
13
14
15
16
  na_inv_inG :> inG Σ (prodR coPset_disjR (gset_disjR positive)).
Definition na_invΣ : gFunctors :=
  #[ GFunctor (constRF (prodR coPset_disjR (gset_disjR positive))) ].
Instance subG_na_invG {Σ} : subG na_invΣ Σ  na_invG Σ.
Proof. solve_inG. Qed.
17
18

Section defs.
19
  Context `{invG Σ, na_invG Σ}.
20

Ralf Jung's avatar
Ralf Jung committed
21
  Definition na_own (p : na_inv_pool_name) (E : coPset) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
22
    own p (CoPset E, GSet ).
23

Ralf Jung's avatar
Ralf Jung committed
24
  Definition na_inv (p : na_inv_pool_name) (N : namespace) (P : iProp Σ) : iProp Σ :=
25
    ( i, i  (N:coPset) 
Robbert Krebbers's avatar
Robbert Krebbers committed
26
          inv N (P  own p (CoPset , GSet {[i]})  na_own p {[i]}))%I.
27
28
End defs.

29
30
Instance: Params (@na_inv) 3.
Typeclasses Opaque na_own na_inv.
31
32

Section proofs.
33
  Context `{invG Σ, na_invG Σ}.
34

35
  Global Instance na_own_timeless p E : Timeless (na_own p E).
36
  Proof. rewrite /na_own; apply _. Qed.
37

38
  Global Instance na_inv_ne p N : NonExpansive (na_inv p N).
39
  Proof. rewrite /na_inv. solve_proper. Qed.
Ralf Jung's avatar
Ralf Jung committed
40
  Global Instance na_inv_proper p N : Proper (() ==> ()) (na_inv p N).
41
42
  Proof. apply (ne_proper _). Qed.

43
  Global Instance na_inv_persistent p N P : Persistent (na_inv p N P).
44
  Proof. rewrite /na_inv; apply _. Qed.
45

Ralf Jung's avatar
Ralf Jung committed
46
  Lemma na_alloc : (|==>  p, na_own p )%I.
47
  Proof. by apply own_alloc. Qed.
48

49
  Lemma na_own_disjoint p E1 E2 : na_own p E1 - na_own p E2 - E1 ## E2.
50
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
51
    apply wand_intro_r.
52
    rewrite /na_own -own_op own_valid -coPset_disj_valid_op. by iIntros ([? _]).
53
54
  Qed.

Ralf Jung's avatar
Ralf Jung committed
55
  Lemma na_own_union p E1 E2 :
56
    E1 ## E2  na_own p (E1  E2)  na_own p E1  na_own p E2.
57
  Proof.
58
    intros ?. by rewrite /na_own -own_op pair_op left_id coPset_disj_union.
59
60
  Qed.

Ralf Jung's avatar
Ralf Jung committed
61
62
63
64
65
66
67
  Lemma na_own_acc E2 E1 tid :
    E2  E1  na_own tid E1 - na_own tid E2  (na_own tid E2 - na_own tid E1).
  Proof.
    intros HF. assert (E1 = E2  (E1  E2)) as -> by exact: union_difference_L.
    rewrite na_own_union; last by set_solver+. iIntros "[$ $]". auto.
  Qed.

Ralf Jung's avatar
Ralf Jung committed
68
  Lemma na_inv_alloc p E N P :  P ={E}= na_inv p N P.
69
70
  Proof.
    iIntros "HP".
Robbert Krebbers's avatar
Robbert Krebbers committed
71
    iMod (own_unit (prodUR coPset_disjUR (gset_disjUR positive)) p) as "Hempty".
72
    iMod (own_updateP with "Hempty") as ([m1 m2]) "[Hm Hown]".
73
    { apply prod_updateP'. apply cmra_updateP_id, (reflexivity (R:=eq)).
74
      apply (gset_disj_alloc_empty_updateP_strong' (λ i, i  (N:coPset))).
75
      intros Ef. exists (coPpick ( N  coPset.of_gset Ef)).
76
77
78
79
      rewrite -coPset.elem_of_of_gset comm -elem_of_difference.
      apply coPpick_elem_of=> Hfin.
      eapply nclose_infinite, (difference_finite_inv _ _), Hfin.
      apply of_gset_finite. }
80
    simpl. iDestruct "Hm" as %(<- & i & -> & ?).
81
    rewrite /na_inv.
82
    iMod (inv_alloc N with "[-]"); last (iModIntro; iExists i; eauto).
83
84
85
    iNext. iLeft. by iFrame.
  Qed.

Ralf Jung's avatar
Ralf Jung committed
86
  Lemma na_inv_open p E F N P :
87
    N  E  N  F 
Ralf Jung's avatar
Ralf Jung committed
88
89
    na_inv p N P - na_own p F ={E}=  P  na_own p (F∖↑N) 
                       ( P  na_own p (F∖↑N) ={E}= na_own p F).
90
  Proof.
91
92
    rewrite /na_inv. iIntros (??) "#Hnainv Htoks".
    iDestruct "Hnainv" as (i) "[% Hinv]".
Ralf Jung's avatar
Ralf Jung committed
93
    rewrite [F as X in na_own p X](union_difference_L (N) F) //.
94
    rewrite [X in (X  _)](union_difference_L {[i]} (N)) ?na_own_union; [|set_solver..].
Robbert Krebbers's avatar
Robbert Krebbers committed
95
    iDestruct "Htoks" as "[[Htoki $] $]".
96
    iInv N as "[[$ >Hdis]|>Htoki2]" "Hclose".
97
98
    - iMod ("Hclose" with "[Htoki]") as "_"; first auto.
      iIntros "!> [HP $]".
99
      iInv N as "[[_ >Hdis2]|>Hitok]" "Hclose".
100
      + iDestruct (own_valid_2 with "Hdis Hdis2") as %[_ Hval%gset_disj_valid_op].
Robbert Krebbers's avatar
Tweak.    
Robbert Krebbers committed
101
        set_solver.
102
      + iFrame. iApply "Hclose". iNext. iLeft. by iFrame.
103
    - iDestruct (na_own_disjoint with "Htoki Htoki2") as %?. set_solver.
104
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
105
End proofs.