wsat.v 8.41 KB
Newer Older
1
From iris.base_logic.lib Require Export own.
Ralf Jung's avatar
Ralf Jung committed
2
From stdpp Require Export coPset.
3
From iris.algebra Require Import gmap auth agree gset coPset.
4
From iris.proofmode Require Import tactics.
5
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
6

7 8 9 10 11 12 13 14 15 16 17 18
Module invG.
  Class invG (Σ : gFunctors) : Set := WsatG {
    inv_inG :> inG Σ (authR (gmapUR positive (agreeR (laterC (iPreProp Σ)))));
    enabled_inG :> inG Σ coPset_disjR;
    disabled_inG :> inG Σ (gset_disjR positive);
    invariant_name : gname;
    enabled_name : gname;
    disabled_name : gname;
  }.
End invG.
Import invG.

19 20 21 22 23 24 25 26 27 28 29 30 31 32
Definition invΣ : gFunctors :=
  #[GFunctor (authRF (gmapURF positive (agreeRF (laterCF idCF))));
    GFunctor coPset_disjUR;
    GFunctor (gset_disjUR positive)].

Class invPreG (Σ : gFunctors) : Set := WsatPreG {
  inv_inPreG :> inG Σ (authR (gmapUR positive (agreeR (laterC (iPreProp Σ)))));
  enabled_inPreG :> inG Σ coPset_disjR;
  disabled_inPreG :> inG Σ (gset_disjR positive);
}.

Instance subG_invΣ {Σ} : subG invΣ Σ  invPreG Σ.
Proof. solve_inG. Qed.

33 34
Definition invariant_unfold {Σ} (P : iProp Σ) : agree (later (iPreProp Σ)) :=
  to_agree (Next (iProp_unfold P)).
35
Definition ownI `{invG Σ} (i : positive) (P : iProp Σ) : iProp Σ :=
36
  own invariant_name ( {[ i := invariant_unfold P ]}).
37
Arguments ownI {_ _} _ _%I.
38
Typeclasses Opaque ownI.
39
Instance: Params (@invariant_unfold) 1.
40
Instance: Params (@ownI) 3.
41

42
Definition ownE `{invG Σ} (E : coPset) : iProp Σ :=
43 44
  own enabled_name (CoPset E).
Typeclasses Opaque ownE.
45
Instance: Params (@ownE) 3.
46

47
Definition ownD `{invG Σ} (E : gset positive) : iProp Σ :=
48 49
  own disabled_name (GSet E).
Typeclasses Opaque ownD.
50
Instance: Params (@ownD) 3.
51

52
Definition wsat `{invG Σ} : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
53
  locked ( I : gmap positive (iProp Σ),
54 55
    own invariant_name ( (invariant_unfold <$> I : gmap _ _)) 
    [ map] i  Q  I,  Q  ownD {[i]}  ownE {[i]})%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
56

57 58
Section wsat.
Context `{invG Σ}.
59 60
Implicit Types P : iProp Σ.

Robbert Krebbers's avatar
Robbert Krebbers committed
61
(* Invariants *)
62 63
Instance invariant_unfold_contractive : Contractive (@invariant_unfold Σ).
Proof. solve_contractive. Qed.
64
Global Instance ownI_contractive i : Contractive (@ownI Σ _ i).
65
Proof. solve_contractive. Qed.
66
Global Instance ownI_persistent i P : Persistent (ownI i P).
67
Proof. rewrite /ownI. apply _. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
68

69
Lemma ownE_empty : (|==> ownE )%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
70
Proof.
Ralf Jung's avatar
Ralf Jung committed
71
  rewrite /uPred_valid /bi_emp_valid.
Robbert Krebbers's avatar
Robbert Krebbers committed
72 73
  by rewrite (own_unit (coPset_disjUR) enabled_name).
Qed.
74
Lemma ownE_op E1 E2 : E1 ## E2  ownE (E1  E2)  ownE E1  ownE E2.
75
Proof. intros. by rewrite /ownE -own_op coPset_disj_union. Qed.
76
Lemma ownE_disjoint E1 E2 : ownE E1  ownE E2  E1 ## E2.
77
Proof. rewrite /ownE -own_op own_valid. by iIntros (?%coPset_disj_valid_op). Qed.
78
Lemma ownE_op' E1 E2 : E1 ## E2  ownE (E1  E2)  ownE E1  ownE E2.
Robbert Krebbers's avatar
Robbert Krebbers committed
79
Proof.
80
  iSplit; [iIntros "[% ?]"; by iApply ownE_op|].
81
  iIntros "HE". iDestruct (ownE_disjoint with "HE") as %?.
82
  iSplit; first done. iApply ownE_op; by try iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
83
Qed.
84
Lemma ownE_singleton_twice i : ownE {[i]}  ownE {[i]}  False.
85
Proof. rewrite ownE_disjoint. iIntros (?); set_solver. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
86

87
Lemma ownD_empty : (|==> ownD )%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
88
Proof.
Ralf Jung's avatar
Ralf Jung committed
89
  rewrite /uPred_valid /bi_emp_valid.
Robbert Krebbers's avatar
Robbert Krebbers committed
90 91
  by rewrite (own_unit (gset_disjUR positive) disabled_name).
Qed.
92
Lemma ownD_op E1 E2 : E1 ## E2  ownD (E1  E2)  ownD E1  ownD E2.
93
Proof. intros. by rewrite /ownD -own_op gset_disj_union. Qed.
94
Lemma ownD_disjoint E1 E2 : ownD E1  ownD E2  E1 ## E2.
95
Proof. rewrite /ownD -own_op own_valid. by iIntros (?%gset_disj_valid_op). Qed.
96
Lemma ownD_op' E1 E2 : E1 ## E2  ownD (E1  E2)  ownD E1  ownD E2.
Robbert Krebbers's avatar
Robbert Krebbers committed
97
Proof.
98
  iSplit; [iIntros "[% ?]"; by iApply ownD_op|].
99
  iIntros "HE". iDestruct (ownD_disjoint with "HE") as %?.
100
  iSplit; first done. iApply ownD_op; by try iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
101
Qed.
102
Lemma ownD_singleton_twice i : ownD {[i]}  ownD {[i]}  False.
103 104
Proof. rewrite ownD_disjoint. iIntros (?); set_solver. Qed.

105
Lemma invariant_lookup (I : gmap positive (iProp Σ)) i P :
106
  own invariant_name ( (invariant_unfold <$> I : gmap _ _)) 
107
  own invariant_name ( {[i := invariant_unfold P]}) 
Ralf Jung's avatar
Ralf Jung committed
108
   Q, I !! i = Some Q   (Q  P).
Robbert Krebbers's avatar
Robbert Krebbers committed
109
Proof.
110
  rewrite -own_op own_valid auth_validI /=. iIntros "[#HI #HvI]".
111 112
  iDestruct "HI" as (I') "HI". rewrite gmap_equivI gmap_validI.
  iSpecialize ("HI" $! i). iSpecialize ("HvI" $! i).
Robbert Krebbers's avatar
Robbert Krebbers committed
113
  rewrite left_id_L lookup_fmap lookup_op lookup_singleton bi.option_equivI.
114 115 116 117 118 119 120
  case: (I !! i)=> [Q|] /=; [|case: (I' !! i)=> [Q'|] /=; by iExFalso].
  iExists Q; iSplit; first done.
  iAssert (invariant_unfold Q  invariant_unfold P)%I as "?".
  { case: (I' !! i)=> [Q'|] //=.
    iRewrite "HI" in "HvI". rewrite uPred.option_validI agree_validI.
    iRewrite -"HvI" in "HI". by rewrite agree_idemp. }
  rewrite /invariant_unfold.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
  by rewrite agree_equivI bi.later_equivI iProp_unfold_equivI.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
Qed.
123

124
Lemma ownI_open i P : wsat  ownI i P  ownE {[i]}  wsat   P  ownD {[i]}.
125
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
126
  rewrite /ownI /wsat -!lock.
127
  iIntros "(Hw & Hi & HiE)". iDestruct "Hw" as (I) "[Hw HI]".
128
  iDestruct (invariant_lookup I i P with "[$]") as (Q ?) "#HPQ".
129
  iDestruct (big_opM_delete _ _ i with "HI") as "[[[HQ $]|HiE'] HI]"; eauto.
130
  - iSplitR "HQ"; last by iNext; iRewrite -"HPQ".
131
    iExists I. iFrame "Hw". iApply (big_opM_delete _ _ i); eauto.
132
    iFrame "HI"; eauto.
133
  - iDestruct (ownE_singleton_twice with "[$HiE $HiE']") as %[].
134
Qed.
135
Lemma ownI_close i P : wsat  ownI i P   P  ownD {[i]}  wsat  ownE {[i]}.
136
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
137
  rewrite /ownI /wsat -!lock.
138
  iIntros "(Hw & Hi & HP & HiD)". iDestruct "Hw" as (I) "[Hw HI]".
139
  iDestruct (invariant_lookup with "[$]") as (Q ?) "#HPQ".
140
  iDestruct (big_opM_delete _ _ i with "HI") as "[[[HQ ?]|$] HI]"; eauto.
141
  - iDestruct (ownD_singleton_twice with "[$]") as %[].
142
  - iExists I. iFrame "Hw". iApply (big_opM_delete _ _ i); eauto.
143 144 145 146 147
    iFrame "HI". iLeft. iFrame "HiD". by iNext; iRewrite "HPQ".
Qed.

Lemma ownI_alloc φ P :
  ( E : gset positive,  i, i  E  φ i) 
Ralf Jung's avatar
Ralf Jung committed
148
  wsat   P ==  i, ⌜φ i  wsat  ownI i P.
Robbert Krebbers's avatar
Robbert Krebbers committed
149
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
150
  iIntros (Hfresh) "[Hw HP]". rewrite /wsat -!lock.
151
  iDestruct "Hw" as (I) "[Hw HI]".
Robbert Krebbers's avatar
Robbert Krebbers committed
152
  iMod (own_unit (gset_disjUR positive) disabled_name) as "HE".
153
  iMod (own_updateP with "[$]") as "HE".
154
  { apply (gset_disj_alloc_empty_updateP_strong' (λ i, I !! i = None  φ i)).
155 156 157
    intros E. destruct (Hfresh (E  dom _ I))
      as (i & [? HIi%not_elem_of_dom]%not_elem_of_union & ?); eauto. }
  iDestruct "HE" as (X) "[Hi HE]"; iDestruct "Hi" as %(i & -> & HIi & ?).
158
  iMod (own_update with "Hw") as "[Hw HiP]".
159 160
  { eapply auth_update_alloc,
     (alloc_singleton_local_update _ i (invariant_unfold P)); last done.
161
    by rewrite /= lookup_fmap HIi. }
162
  iModIntro; iExists i;  iSplit; [done|]. rewrite /ownI; iFrame "HiP".
163 164
  iExists (<[i:=P]>I); iSplitL "Hw".
  { by rewrite fmap_insert insert_singleton_op ?lookup_fmap ?HIi. }
165
  iApply (big_opM_insert _ I); first done.
166
  iFrame "HI". iLeft. by rewrite /ownD; iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
167
Qed.
168 169 170 171 172

Lemma ownI_alloc_open φ P :
  ( E : gset positive,  i, i  E  φ i) 
  wsat ==  i, ⌜φ i  (ownE {[i]} - wsat)  ownI i P  ownD {[i]}.
Proof.
173
  iIntros (Hfresh) "Hw". rewrite /wsat -!lock. iDestruct "Hw" as (I) "[Hw HI]".
Robbert Krebbers's avatar
Robbert Krebbers committed
174
  iMod (own_unit (gset_disjUR positive) disabled_name) as "HD".
175
  iMod (own_updateP with "[$]") as "HD".
176 177 178 179 180 181 182 183 184 185 186 187
  { apply (gset_disj_alloc_empty_updateP_strong' (λ i, I !! i = None  φ i)).
    intros E. destruct (Hfresh (E  dom _ I))
      as (i & [? HIi%not_elem_of_dom]%not_elem_of_union & ?); eauto. }
  iDestruct "HD" as (X) "[Hi HD]"; iDestruct "Hi" as %(i & -> & HIi & ?).
  iMod (own_update with "Hw") as "[Hw HiP]".
  { eapply auth_update_alloc,
     (alloc_singleton_local_update _ i (invariant_unfold P)); last done.
    by rewrite /= lookup_fmap HIi. }
  iModIntro; iExists i;  iSplit; [done|]. rewrite /ownI; iFrame "HiP".
  rewrite -/(ownD _). iFrame "HD".
  iIntros "HE". iExists (<[i:=P]>I); iSplitL "Hw".
  { by rewrite fmap_insert insert_singleton_op ?lookup_fmap ?HIi. }
188
  iApply (big_opM_insert _ I); first done.
189 190
  iFrame "HI". by iRight.
Qed.
191
End wsat.
192 193 194 195 196 197 198 199 200 201 202 203

(* Allocation of an initial world *)
Lemma wsat_alloc `{invPreG Σ} : (|==>  _ : invG Σ, wsat  ownE )%I.
Proof.
  iIntros.
  iMod (own_alloc ( ( : gmap _ _))) as (γI) "HI"; first done.
  iMod (own_alloc (CoPset )) as (γE) "HE"; first done.
  iMod (own_alloc (GSet )) as (γD) "HD"; first done.
  iModIntro; iExists (WsatG _ _ _ _ γI γE γD).
  rewrite /wsat /ownE -lock; iFrame.
  iExists . rewrite fmap_empty big_opM_empty. by iFrame.
Qed.