wsat.v 6.3 KB
Newer Older
1
2
From iris.base_logic.lib Require Export own.
From iris.prelude Require Export coPset.
3
4
From iris.algebra Require Import gmap auth agree gset coPset.
From iris.base_logic Require Import big_op.
5
From iris.proofmode Require Import tactics.
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
Definition invariant_unfold {Σ} (P : iProp Σ) : agree (later (iPreProp Σ)) :=
  to_agree (Next (iProp_unfold P)).
21
Definition ownI `{invG Σ} (i : positive) (P : iProp Σ) : iProp Σ :=
22
  own invariant_name ( {[ i := invariant_unfold P ]}).
23
Arguments ownI {_ _} _ _%I.
24
Typeclasses Opaque ownI.
25
Instance: Params (@ownI) 3.
26

27
Definition ownE `{invG Σ} (E : coPset) : iProp Σ :=
28
29
  own enabled_name (CoPset E).
Typeclasses Opaque ownE.
30
Instance: Params (@ownE) 3.
31

32
Definition ownD `{invG Σ} (E : gset positive) : iProp Σ :=
33
34
  own disabled_name (GSet E).
Typeclasses Opaque ownD.
35
Instance: Params (@ownD) 3.
36

37
Definition wsat `{invG Σ} : iProp Σ :=
38
  ( I : gmap positive (iProp Σ),
39
40
    own invariant_name ( (invariant_unfold <$> I : gmap _ _)) 
    [ map] i  Q  I,  Q  ownD {[i]}  ownE {[i]})%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
41

42
43
Section wsat.
Context `{invG Σ}.
44
45
Implicit Types P : iProp Σ.

Robbert Krebbers's avatar
Robbert Krebbers committed
46
(* Invariants *)
47
48
Instance invariant_unfold_contractive : Contractive (@invariant_unfold Σ).
Proof. solve_contractive. Qed.
49
Global Instance ownI_contractive i : Contractive (@ownI Σ _ i).
50
Proof. solve_contractive. Qed.
51
52
Global Instance ownI_persistent i P : PersistentP (ownI i P).
Proof. rewrite /ownI. apply _. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
53

54
55
Lemma ownE_empty : (|==> ownE )%I.
Proof. by rewrite /uPred_valid (own_empty (coPset_disjUR) enabled_name). Qed.
56
Lemma ownE_op E1 E2 : E1  E2  ownE (E1  E2)  ownE E1  ownE E2.
57
Proof. intros. by rewrite /ownE -own_op coPset_disj_union. Qed.
Ralf Jung's avatar
Ralf Jung committed
58
Lemma ownE_disjoint E1 E2 : ownE E1  ownE E2  E1  E2.
59
Proof. rewrite /ownE -own_op own_valid. by iIntros (?%coPset_disj_valid_op). Qed.
Ralf Jung's avatar
Ralf Jung committed
60
Lemma ownE_op' E1 E2 : E1  E2  ownE (E1  E2)  ownE E1  ownE E2.
Robbert Krebbers's avatar
Robbert Krebbers committed
61
Proof.
62
  iSplit; [iIntros "[% ?]"; by iApply ownE_op|].
63
  iIntros "HE". iDestruct (ownE_disjoint with "HE") as %?.
64
  iSplit; first done. iApply ownE_op; by try iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
65
Qed.
66
Lemma ownE_singleton_twice i : ownE {[i]}  ownE {[i]}  False.
67
Proof. rewrite ownE_disjoint. iIntros (?); set_solver. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
68

69
70
Lemma ownD_empty : (|==> ownD )%I.
Proof. by rewrite /uPred_valid (own_empty (gset_disjUR positive) disabled_name). Qed.
71
Lemma ownD_op E1 E2 : E1  E2  ownD (E1  E2)  ownD E1  ownD E2.
72
Proof. intros. by rewrite /ownD -own_op gset_disj_union. Qed.
Ralf Jung's avatar
Ralf Jung committed
73
Lemma ownD_disjoint E1 E2 : ownD E1  ownD E2  E1  E2.
74
Proof. rewrite /ownD -own_op own_valid. by iIntros (?%gset_disj_valid_op). Qed.
Ralf Jung's avatar
Ralf Jung committed
75
Lemma ownD_op' E1 E2 : E1  E2  ownD (E1  E2)  ownD E1  ownD E2.
Robbert Krebbers's avatar
Robbert Krebbers committed
76
Proof.
77
  iSplit; [iIntros "[% ?]"; by iApply ownD_op|].
78
  iIntros "HE". iDestruct (ownD_disjoint with "HE") as %?.
79
  iSplit; first done. iApply ownD_op; by try iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
80
Qed.
81
Lemma ownD_singleton_twice i : ownD {[i]}  ownD {[i]}  False.
82
83
Proof. rewrite ownD_disjoint. iIntros (?); set_solver. Qed.

84
Lemma invariant_lookup (I : gmap positive (iProp Σ)) i P :
85
  own invariant_name ( (invariant_unfold <$> I : gmap _ _)) 
86
  own invariant_name ( {[i := invariant_unfold P]}) 
Ralf Jung's avatar
Ralf Jung committed
87
   Q, I !! i = Some Q   (Q  P).
Robbert Krebbers's avatar
Robbert Krebbers committed
88
Proof.
89
  rewrite -own_op own_valid auth_validI /=. iIntros "[#HI #HvI]".
90
91
92
93
94
95
96
97
98
99
  iDestruct "HI" as (I') "HI". rewrite gmap_equivI gmap_validI.
  iSpecialize ("HI" $! i). iSpecialize ("HvI" $! i).
  rewrite left_id_L lookup_fmap lookup_op lookup_singleton uPred.option_equivI.
  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.
100
  by rewrite agree_equivI uPred.later_equivI iProp_unfold_equivI.
Robbert Krebbers's avatar
Robbert Krebbers committed
101
Qed.
102

103
Lemma ownI_open i P : wsat  ownI i P  ownE {[i]}  wsat   P  ownD {[i]}.
104
105
Proof.
  rewrite /ownI. iIntros "(Hw & Hi & HiE)". iDestruct "Hw" as (I) "[? HI]".
106
107
  iDestruct (invariant_lookup I i P with "[$Hw $Hi]") as (Q) "[% #HPQ]".
  iDestruct (big_sepM_delete _ _ i with "HI") as "[[[HQ $]|HiE'] HI]"; eauto.
108
109
110
  - iSplitR "HQ"; last by iNext; iRewrite -"HPQ".
    iExists I. iFrame "Hw". iApply (big_sepM_delete _ _ i); eauto.
    iFrame "HI"; eauto.
111
  - iDestruct (ownE_singleton_twice with "[$HiE $HiE']") as %[].
112
Qed.
113
Lemma ownI_close i P : wsat  ownI i P   P  ownD {[i]}  wsat  ownE {[i]}.
114
115
Proof.
  rewrite /ownI. iIntros "(Hw & Hi & HP & HiD)". iDestruct "Hw" as (I) "[? HI]".
116
  iDestruct (invariant_lookup with "[$Hw $Hi]") as (Q) "[% #HPQ]".
117
  iDestruct (big_sepM_delete _ _ i with "HI") as "[[[HQ ?]|$] HI]"; eauto.
118
  - iDestruct (ownD_singleton_twice with "[-]") as %[]. by iFrame.
119
120
121
122
123
124
  - iExists I. iFrame "Hw". iApply (big_sepM_delete _ _ i); eauto.
    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
125
  wsat   P ==  i, ⌜φ i  wsat  ownI i P.
Robbert Krebbers's avatar
Robbert Krebbers committed
126
Proof.
127
  iIntros (Hfresh) "[Hw HP]". iDestruct "Hw" as (I) "[? HI]".
128
  iMod (own_empty (gset_disjUR positive) disabled_name) as "HE".
129
  iMod (own_updateP with "HE") as "HE".
130
  { apply (gset_disj_alloc_empty_updateP_strong' (λ i, I !! i = None  φ i)).
131
132
133
    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 & ?).
134
  iMod (own_update with "Hw") as "[Hw HiP]".
135
136
  { eapply auth_update_alloc,
     (alloc_singleton_local_update _ i (invariant_unfold P)); last done.
137
    by rewrite /= lookup_fmap HIi. }
138
  iModIntro; iExists i;  iSplit; [done|]. rewrite /ownI; iFrame "HiP".
139
140
141
142
  iExists (<[i:=P]>I); iSplitL "Hw".
  { by rewrite fmap_insert insert_singleton_op ?lookup_fmap ?HIi. }
  iApply (big_sepM_insert _ I); first done.
  iFrame "HI". iLeft. by rewrite /ownD; iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
143
Qed.
144
End wsat.