sts.v 7.22 KB
Newer Older
1
From iris.proofmode Require Import tactics.
2 3
From iris.algebra Require Export sts.
From iris.base_logic.lib Require Export invariants.
4
Set Default Proof Using "Type".
Ralf Jung's avatar
Ralf Jung committed
5 6
Import uPred.

7
(** The CMRA we need. *)
8 9
Class stsG Σ (sts : stsT) := StsG {
  sts_inG :> inG Σ (stsR sts);
Robbert Krebbers's avatar
Robbert Krebbers committed
10
  sts_inhabited :> Inhabited (sts.state sts);
Ralf Jung's avatar
Ralf Jung committed
11
}.
12

13
Definition stsΣ (sts : stsT) : gFunctors := #[ GFunctor (stsR sts) ].
14 15
Instance subG_stsΣ Σ sts :
  subG (stsΣ sts) Σ  Inhabited (sts.state sts)  stsG Σ sts.
16
Proof. solve_inG. Qed.
17

18
Section definitions.
19
  Context `{!stsG Σ sts} (γ : gname).
20 21

  Definition sts_ownS (S : sts.states sts) (T : sts.tokens sts) : iProp Σ :=
22
    own γ (sts_frag S T).
23
  Definition sts_own (s : sts.state sts) (T : sts.tokens sts) : iProp Σ :=
24
    own γ (sts_frag_up s T).
25
  Definition sts_inv (φ : sts.state sts  iProp Σ) : iProp Σ :=
26
    ( s, own γ (sts_auth s )  φ s)%I.
27
  Definition sts_ctx `{!invG Σ} (N : namespace) (φ: sts.state sts  iProp Σ) : iProp Σ :=
28
    inv N (sts_inv φ).
Ralf Jung's avatar
Ralf Jung committed
29

30 31 32 33 34 35
  Global Instance sts_inv_ne n :
    Proper (pointwise_relation _ (dist n) ==> dist n) sts_inv.
  Proof. solve_proper. Qed.
  Global Instance sts_inv_proper :
    Proper (pointwise_relation _ () ==> ()) sts_inv.
  Proof. solve_proper. Qed.
36
  Global Instance sts_ownS_proper : Proper (() ==> () ==> ()) sts_ownS.
37
  Proof. solve_proper. Qed.
38
  Global Instance sts_own_proper s : Proper (() ==> ()) (sts_own s).
39
  Proof. solve_proper. Qed.
40
  Global Instance sts_ctx_ne `{!invG Σ} n N :
41 42
    Proper (pointwise_relation _ (dist n) ==> dist n) (sts_ctx N).
  Proof. solve_proper. Qed.
43
  Global Instance sts_ctx_proper `{!invG Σ} N :
44
    Proper (pointwise_relation _ () ==> ()) (sts_ctx N).
45
  Proof. solve_proper. Qed.
46
  Global Instance sts_ctx_persistent `{!invG Σ} N φ : Persistent (sts_ctx N φ).
47
  Proof. apply _. Qed.
48
  Global Instance sts_own_persistent s : Persistent (sts_own s ).
49
  Proof. apply _. Qed.
50
  Global Instance sts_ownS_persistent S : Persistent (sts_ownS S ).
51
  Proof. apply _. Qed.
52
End definitions.
53

54 55 56 57
Instance: Params (@sts_inv) 4 := {}.
Instance: Params (@sts_ownS) 4 := {}.
Instance: Params (@sts_own) 5 := {}.
Instance: Params (@sts_ctx) 6 := {}.
Ralf Jung's avatar
Ralf Jung committed
58 59

Section sts.
60
  Context `{!invG Σ, !stsG Σ sts}.
61
  Implicit Types φ : sts.state sts  iProp Σ.
Ralf Jung's avatar
Ralf Jung committed
62
  Implicit Types N : namespace.
63
  Implicit Types P Q R : iProp Σ.
Ralf Jung's avatar
Ralf Jung committed
64
  Implicit Types γ : gname.
Robbert Krebbers's avatar
Robbert Krebbers committed
65 66 67
  Implicit Types S : sts.states sts.
  Implicit Types T : sts.tokens sts.

68 69
  (* The same rule as implication does *not* hold, as could be shown using
     sts_frag_included. *)
70
  Lemma sts_ownS_weaken γ S1 S2 T1 T2 :
71
    T2  T1  S1  S2  sts.closed S2 T2 
72
    sts_ownS γ S1 T1 == sts_ownS γ S2 T2.
73
  Proof. intros ???. by apply own_update, sts_update_frag. Qed.
74

75
  Lemma sts_own_weaken γ s S T1 T2 :
76
    T2  T1  s  S  sts.closed S T2 
77
    sts_own γ s T1 == sts_ownS γ S T2.
78
  Proof. intros ???. by apply own_update, sts_update_frag_up. Qed.
79

Ralf Jung's avatar
Ralf Jung committed
80
  Lemma sts_own_weaken_state γ s1 s2 T :
81
    sts.frame_steps T s2 s1  sts.tok s2 ## T 
Ralf Jung's avatar
Ralf Jung committed
82 83 84
    sts_own γ s1 T == sts_own γ s2 T.
  Proof.
    intros ??. apply own_update, sts_update_frag_up; [|done..].
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
85
    intros Hdisj. apply sts.closed_up. done.
Ralf Jung's avatar
Ralf Jung committed
86 87 88 89 90 91 92 93 94 95
  Qed.

  Lemma sts_own_weaken_tok γ s T1 T2 :
    T2  T1  sts_own γ s T1 == sts_own γ s T2.
  Proof.
    intros ?. apply own_update, sts_update_frag_up; last done.
    - intros. apply sts.closed_up. set_solver.
    - apply sts.elem_of_up.
  Qed.

Ralf Jung's avatar
Ralf Jung committed
96
  Lemma sts_ownS_op γ S1 S2 T1 T2 :
97
    T1 ## T2  sts.closed S1 T1  sts.closed S2 T2 
98
    sts_ownS γ (S1  S2) (T1  T2)  (sts_ownS γ S1 T1  sts_ownS γ S2 T2).
99
  Proof. intros. by rewrite /sts_ownS -own_op sts_op_frag. Qed.
Ralf Jung's avatar
Ralf Jung committed
100

Ralf Jung's avatar
Ralf Jung committed
101
  Lemma sts_own_op γ s T1 T2 :
102
    T1 ## T2  sts_own γ s (T1  T2) == sts_own γ s T1  sts_own γ s T2.
Ralf Jung's avatar
Ralf Jung committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    (* The other direction does not hold -- see sts.up_op. *)
  Proof.
    intros. rewrite /sts_own -own_op. iIntros "Hown".
    iDestruct (own_valid with "Hown") as %Hval%sts_frag_up_valid.
    rewrite -sts_op_frag.
    - iApply (sts_own_weaken with "Hown"); first done.
      + split; apply sts.elem_of_up.
      + eapply sts.closed_op; apply sts.closed_up; set_solver.
    - done.
    - apply sts.closed_up; set_solver.
    - apply sts.closed_up; set_solver.
  Qed.

  Typeclasses Opaque sts_own sts_ownS sts_inv sts_ctx.

118
  Lemma sts_alloc φ E N s :
119
     φ s ={E}=  γ, sts_ctx γ N φ  sts_own γ s (  sts.tok s).
120
  Proof.
121
    iIntros "Hφ". rewrite /sts_ctx /sts_own.
122
    iMod (own_alloc (sts_auth s (  sts.tok s))) as (γ) "Hγ".
123 124
    { apply sts_auth_valid; set_solver. }
    iExists γ; iRevert "Hγ"; rewrite -sts_op_auth_frag_up; iIntros "[Hγ $]".
125
    iMod (inv_alloc N _ (sts_inv γ φ) with "[Hφ Hγ]") as "#?"; auto.
Ralf Jung's avatar
Ralf Jung committed
126
    rewrite /sts_inv. iNext. iExists s. by iFrame.
Ralf Jung's avatar
Ralf Jung committed
127
  Qed.
128

129
  Lemma sts_accS φ E γ S T :
130
     sts_inv γ φ  sts_ownS γ S T ={E}=  s,
Ralf Jung's avatar
Ralf Jung committed
131 132
      s  S   φ s   s' T',
      sts.steps (s, T) (s', T')   φ s' ={E}=  sts_inv γ φ  sts_own γ s' T'.
133
  Proof.
Ralf Jung's avatar
Ralf Jung committed
134 135
    iIntros "[Hinv Hγf]". rewrite /sts_ownS /sts_inv /sts_own.
    iDestruct "Hinv" as (s) "[>Hγ Hφ]".
Robbert Krebbers's avatar
Robbert Krebbers committed
136
    iDestruct (own_valid_2 with "Hγ Hγf") as %Hvalid.
137 138
    assert (s  S) by eauto using sts_auth_frag_valid_inv.
    assert ( sts_frag S T) as [??] by eauto using cmra_valid_op_r.
139
    iModIntro; iExists s; iSplit; [done|]; iFrame "Hφ".
140
    iIntros (s' T') "[% Hφ]".
Robbert Krebbers's avatar
Robbert Krebbers committed
141 142
    iMod (own_update_2 with "Hγ Hγf") as "Hγ".
    { rewrite sts_op_auth_frag; [|done..]. by apply sts_update_auth. }
143
    iRevert "Hγ"; rewrite -sts_op_auth_frag_up; iIntros "[Hγ $]".
144
    iModIntro. iNext. iExists s'; by iFrame.
Ralf Jung's avatar
Ralf Jung committed
145 146
  Qed.

147
  Lemma sts_acc φ E γ s0 T :
148
     sts_inv γ φ  sts_own γ s0 T ={E}=  s,
Ralf Jung's avatar
Ralf Jung committed
149 150
      sts.frame_steps T s0 s   φ s   s' T',
      sts.steps (s, T) (s', T')   φ s' ={E}=  sts_inv γ φ  sts_own γ s' T'.
Ralf Jung's avatar
Ralf Jung committed
151
  Proof. by apply sts_accS. Qed.
152

153
  Lemma sts_openS φ E N γ S T :
154 155
    N  E 
    sts_ctx γ N φ  sts_ownS γ S T ={E,E∖↑N}=  s,
Ralf Jung's avatar
Ralf Jung committed
156
      s  S   φ s   s' T',
157
      sts.steps (s, T) (s', T')   φ s' ={E∖↑N,E}= sts_own γ s' T'.
Ralf Jung's avatar
Ralf Jung committed
158 159 160 161 162 163 164 165
  Proof.
    iIntros (?) "[#? Hγf]". rewrite /sts_ctx. iInv N as "Hinv" "Hclose".
    (* The following is essentially a very trivial composition of the accessors
       [sts_acc] and [inv_open] -- but since we don't have any good support
       for that currently, this gets more tedious than it should, with us having
       to unpack and repack various proofs.
       TODO: Make this mostly automatic, by supporting "opening accessors
       around accessors". *)
166 167 168
    iMod (sts_accS with "[Hinv Hγf]") as (s) "(?&?& HclSts)"; first by iFrame.
    iModIntro. iExists s. iFrame. iIntros (s' T') "H".
    iMod ("HclSts" $! s' T' with "H") as "(Hinv & ?)". by iMod ("Hclose" with "Hinv").
169 170
  Qed.

171
  Lemma sts_open φ E N γ s0 T :
172 173
    N  E 
    sts_ctx γ N φ  sts_own γ s0 T ={E,E∖↑N}=  s,
Ralf Jung's avatar
Ralf Jung committed
174
      sts.frame_steps T s0 s   φ s   s' T',
175
      sts.steps (s, T) (s', T')   φ s' ={E∖↑N,E}= sts_own γ s' T'.
176
  Proof. by apply sts_openS. Qed.
Ralf Jung's avatar
Ralf Jung committed
177
End sts.
Ralf Jung's avatar
Ralf Jung committed
178 179

Typeclasses Opaque sts_own sts_ownS sts_inv sts_ctx.