sts.v 6.96 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
From algebra Require Export sts upred_tactics.
2
From program_logic Require Export invariants global_functor.
Ralf Jung's avatar
Ralf Jung committed
3 4
Import uPred.

5 6
Class stsG Λ Σ (sts : stsT) := StsG {
  sts_inG :> inG Λ Σ (stsRA sts);
Robbert Krebbers's avatar
Robbert Krebbers committed
7
  sts_inhabited :> Inhabited (sts.state sts);
Ralf Jung's avatar
Ralf Jung committed
8
}.
9
Coercion sts_inG : stsG >-> inG.
Ralf Jung's avatar
Ralf Jung committed
10

11
Definition stsGF (sts : stsT) : iFunctor := constF (stsRA sts).
12
Instance inGF_stsG sts `{inGF Λ Σ (stsGF sts)}
13 14
  `{Inhabited (sts.state sts)} : stsG Λ Σ sts.
Proof. split; try apply _. apply: inGF_inG. Qed.
15

Ralf Jung's avatar
Ralf Jung committed
16
Section definitions.
17
  Context `{i : stsG Λ Σ sts} (γ : gname).
Robbert Krebbers's avatar
Robbert Krebbers committed
18 19
  Import sts.
  Definition sts_inv (φ : state sts  iPropG Λ Σ) : iPropG Λ Σ :=
20
    ( s, own γ (sts_auth s )  φ s)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
21
  Definition sts_ownS (S : states sts) (T : tokens sts) : iPropG Λ Σ:=
22
    own γ (sts_frag S T).
Robbert Krebbers's avatar
Robbert Krebbers committed
23
  Definition sts_own (s : state sts) (T : tokens sts) : iPropG Λ Σ :=
24
    own γ (sts_frag_up s T).
Robbert Krebbers's avatar
Robbert Krebbers committed
25 26
  Definition sts_ctx (N : namespace) (φ: state sts  iPropG Λ Σ) : iPropG Λ Σ :=
    inv N (sts_inv φ).
Ralf Jung's avatar
Ralf Jung committed
27
End definitions.
28 29 30 31
Instance: Params (@sts_inv) 5.
Instance: Params (@sts_ownS) 5.
Instance: Params (@sts_own) 6.
Instance: Params (@sts_ctx) 6.
Ralf Jung's avatar
Ralf Jung committed
32 33

Section sts.
34
  Context `{stsG Λ Σ sts} (φ : sts.state sts  iPropG Λ Σ).
Ralf Jung's avatar
Ralf Jung committed
35 36 37
  Implicit Types N : namespace.
  Implicit Types P Q R : iPropG Λ Σ.
  Implicit Types γ : gname.
Robbert Krebbers's avatar
Robbert Krebbers committed
38 39 40 41 42
  Implicit Types S : sts.states sts.
  Implicit Types T : sts.tokens sts.

  (** Setoids *)
  Global Instance sts_inv_ne n γ :
43
    Proper (pointwise_relation _ (dist n) ==> dist n) (sts_inv γ).
Robbert Krebbers's avatar
Robbert Krebbers committed
44 45
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_inv; setoid_rewrite Hφ. Qed.
  Global Instance sts_inv_proper γ :
46
    Proper (pointwise_relation _ () ==> ()) (sts_inv γ).
Robbert Krebbers's avatar
Robbert Krebbers committed
47
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_inv; setoid_rewrite Hφ. Qed.
48
  Global Instance sts_ownS_proper γ : Proper (() ==> () ==> ()) (sts_ownS γ).
Robbert Krebbers's avatar
Robbert Krebbers committed
49
  Proof. intros S1 S2 HS T1 T2 HT. by rewrite /sts_ownS HS HT. Qed.
50 51
  Global Instance sts_own_proper γ s : Proper (() ==> ()) (sts_own γ s).
  Proof. intros T1 T2 HT. by rewrite /sts_own HT. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
52
  Global Instance sts_ctx_ne n γ N :
53
    Proper (pointwise_relation _ (dist n) ==> dist n) (sts_ctx γ N).
Robbert Krebbers's avatar
Robbert Krebbers committed
54 55
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_ctx Hφ. Qed.
  Global Instance sts_ctx_proper γ N :
56
    Proper (pointwise_relation _ () ==> ()) (sts_ctx γ N).
Robbert Krebbers's avatar
Robbert Krebbers committed
57
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_ctx Hφ. Qed.
Ralf Jung's avatar
Ralf Jung committed
58

59 60
  (* The same rule as implication does *not* hold, as could be shown using
     sts_frag_included. *)
Ralf Jung's avatar
Ralf Jung committed
61
  Lemma sts_ownS_weaken E γ S1 S2 T1 T2 :
62
    T2  T1  S1  S2  sts.closed S2 T2 
63
    sts_ownS γ S1 T1  (|={E}=> sts_ownS γ S2 T2).
64
  Proof. intros ? ? ?. by apply own_update, sts_update_frag. Qed.
65

Ralf Jung's avatar
Ralf Jung committed
66
  Lemma sts_own_weaken E γ s S T1 T2 :
67
    T2  T1  s  S  sts.closed S T2 
68
    sts_own γ s T1  (|={E}=> sts_ownS γ S T2).
69
  Proof. intros ???. by apply own_update, sts_update_frag_up. Qed.
70

Ralf Jung's avatar
Ralf Jung committed
71
  Lemma sts_ownS_op γ S1 S2 T1 T2 :
Ralf Jung's avatar
Ralf Jung committed
72
    T1  T2    sts.closed S1 T1  sts.closed S2 T2 
Ralf Jung's avatar
Ralf Jung committed
73
    sts_ownS γ (S1  S2) (T1  T2)  (sts_ownS γ S1 T1  sts_ownS γ S2 T2)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
74
  Proof. intros. by rewrite /sts_ownS -own_op sts_op_frag. Qed.
Ralf Jung's avatar
Ralf Jung committed
75

76 77
  Lemma sts_alloc E N s :
    nclose N  E 
78
     φ s  (|={E}=>  γ, sts_ctx γ N φ  sts_own γ s (  sts.tok s)).
79
  Proof.
80
    intros HN. eapply sep_elim_True_r.
81
    { apply (own_alloc (sts_auth s (  sts.tok s)) N).
82
      apply sts_auth_valid; set_solver. }
83 84
    rewrite pvs_frame_l. rewrite -(pvs_mask_weaken N E) //.
    apply pvs_strip_pvs.
85
    rewrite sep_exist_l. apply exist_elim=>γ. rewrite -(exist_intro γ).
86
    trans ( sts_inv γ φ  sts_own γ s (  sts.tok s))%I.
87
    { rewrite /sts_inv -(exist_intro s) later_sep.
88
      ecancel [ φ _]%I.
89
      by rewrite -later_intro -own_op sts_op_auth_frag_up; last set_solver. }
Robbert Krebbers's avatar
Robbert Krebbers committed
90
    rewrite (inv_alloc N) /sts_ctx pvs_frame_r.
91 92 93
    by rewrite always_and_sep_l.
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
94
  Lemma sts_opened E γ S T :
95
    ( sts_inv γ φ  sts_ownS γ S T)
96
     (|={E}=>  s,  (s  S)   φ s  own γ (sts_auth s T)).
97
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
98
    rewrite /sts_inv /sts_ownS later_exist sep_exist_r. apply exist_elim=>s.
99 100 101 102
    rewrite later_sep pvs_timeless !pvs_frame_r. apply pvs_mono.
    rewrite -(exist_intro s).
    rewrite [(_  ▷φ _)%I]comm -!assoc -own_op -[(▷φ _  _)%I]comm.
    rewrite own_valid_l discrete_validI.
Robbert Krebbers's avatar
Robbert Krebbers committed
103 104 105
    rewrite -!assoc. apply const_elim_sep_l=> Hvalid.
    assert (s  S) by (by eapply sts_auth_frag_valid_inv, discrete_valid).
    rewrite const_equiv // left_id comm sts_op_auth_frag //.
106 107 108
    assert ( sts_frag S T) as Hv by
          by eapply cmra_valid_op_r, discrete_valid.
    apply (Hv 0).
109 110
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
111
  Lemma sts_closing E γ s T s' T' :
112
    sts.steps (s, T) (s', T') 
113
    ( φ s'  own γ (sts_auth s T))  (|={E}=>  sts_inv γ φ  sts_own γ s' T').
114
  Proof.
Ralf Jung's avatar
Ralf Jung committed
115 116
    intros Hstep. rewrite /sts_inv /sts_own -(exist_intro s') later_sep.
    (* TODO it would be really nice to use cancel here *)
117
    rewrite [(_   φ _)%I]comm -assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
118
    rewrite -pvs_frame_l. apply sep_mono_r. rewrite -later_intro.
Ralf Jung's avatar
Ralf Jung committed
119
    rewrite own_valid_l discrete_validI. apply const_elim_sep_l=>Hval.
120
    trans (|={E}=> own γ (sts_auth s' T'))%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
    { by apply own_update, sts_update_auth. }
122
    by rewrite -own_op sts_op_auth_frag_up.
Ralf Jung's avatar
Ralf Jung committed
123
  Qed.
124

125 126
  Context {V} (fsa : FSA Λ (globalF Σ) V) `{!FrameShiftAssertion fsaV fsa}.

127
  Lemma sts_fsaS E N P (Ψ : V  iPropG Λ Σ) γ S T :
Ralf Jung's avatar
Ralf Jung committed
128
    fsaV  nclose N  E 
129 130
    P  sts_ctx γ N φ 
    P  (sts_ownS γ S T   s,
131
           (s  S)   φ s -
Ralf Jung's avatar
Ralf Jung committed
132
          fsa (E  nclose N) (λ x,  s' T',
133
             sts.steps (s, T) (s', T')   φ s' 
134 135
            (sts_own γ s' T' - Ψ x))) 
    P  fsa E Ψ.
136
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
137
    rewrite /sts_ctx=>? HN Hinv Hinner.
138 139
    eapply (inv_fsa fsa); eauto. rewrite Hinner=>{Hinner Hinv P HN}.
    apply wand_intro_l. rewrite assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
140
    rewrite (sts_opened (E  N)) !pvs_frame_r !sep_exist_r.
141 142
    apply (fsa_strip_pvs fsa). apply exist_elim=>s.
    rewrite (forall_elim s). rewrite [(_  _)%I]comm.
143 144 145
    eapply wand_apply_r; first (by eapply (wand_frame_l (own γ _))); last first.
    { rewrite assoc [(_  own _ _)%I]comm -assoc. done. }
    rewrite fsa_frame_l.
Ralf Jung's avatar
Ralf Jung committed
146
    apply (fsa_mono_pvs fsa)=> x.
147
    rewrite sep_exist_l; apply exist_elim=> s'.
Ralf Jung's avatar
Ralf Jung committed
148 149
    rewrite sep_exist_l; apply exist_elim=>T'.
    rewrite comm -!assoc. apply const_elim_sep_l=>-Hstep.
150
    rewrite assoc [(_  (_ - _))%I]comm -assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
151
    rewrite (sts_closing (E  N)) //; [].
152 153 154 155
    rewrite pvs_frame_l. apply pvs_mono.
    by rewrite assoc [(_  _)%I]comm -assoc wand_elim_l.
  Qed.

156
  Lemma sts_fsa E N P (Ψ : V  iPropG Λ Σ) γ s0 T :
Ralf Jung's avatar
Ralf Jung committed
157
    fsaV  nclose N  E 
158 159
    P  sts_ctx γ N φ 
    P  (sts_own γ s0 T   s,
Robbert Krebbers's avatar
Robbert Krebbers committed
160
           (s  sts.up s0 T)   φ s -
Ralf Jung's avatar
Ralf Jung committed
161
          fsa (E  nclose N) (λ x,  s' T',
162
             (sts.steps (s, T) (s', T'))   φ s' 
163 164
            (sts_own γ s' T' - Ψ x))) 
    P  fsa E Ψ.
Robbert Krebbers's avatar
Robbert Krebbers committed
165
  Proof. apply sts_fsaS. Qed.
Ralf Jung's avatar
Ralf Jung committed
166
End sts.