sts.v 7.69 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
Definition sts_ownS_def `{i : stsG Λ Σ sts} (γ : gname)
Robbert Krebbers's avatar
Robbert Krebbers committed
17
    (S : sts.states sts) (T : sts.tokens sts) : iPropG Λ Σ:=
Ralf Jung's avatar
Ralf Jung committed
18
  own γ (sts_frag S T).
19
(* Perform sealing. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
20 21 22
Definition sts_ownS_aux : { x | x = @sts_ownS_def }. by eexists. Qed.
Definition sts_ownS {Λ Σ sts i} := proj1_sig sts_ownS_aux Λ Σ sts i.
Definition sts_ownS_eq : @sts_ownS = @sts_ownS_def := proj2_sig sts_ownS_aux.
23

Ralf Jung's avatar
Ralf Jung committed
24
Definition sts_own_def `{i : stsG Λ Σ sts} (γ : gname)
Robbert Krebbers's avatar
Robbert Krebbers committed
25
    (s : sts.state sts) (T : sts.tokens sts) : iPropG Λ Σ :=
Ralf Jung's avatar
Ralf Jung committed
26 27
  own γ (sts_frag_up s T).
(* Perform sealing. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
28 29 30
Definition sts_own_aux : { x | x = @sts_own_def }. by eexists. Qed.
Definition sts_own {Λ Σ sts i} := proj1_sig sts_own_aux Λ Σ sts i.
Definition sts_own_eq : @sts_own = @sts_own_def := proj2_sig sts_own_aux.
Ralf Jung's avatar
Ralf Jung committed
31 32

Definition sts_inv `{i : stsG Λ Σ sts} (γ : gname)
Robbert Krebbers's avatar
Robbert Krebbers committed
33
    (φ : sts.state sts  iPropG Λ Σ) : iPropG Λ Σ :=
Ralf Jung's avatar
Ralf Jung committed
34 35
  ( s, own γ (sts_auth s )  φ s)%I.
Definition sts_ctx `{i : stsG Λ Σ sts} (γ : gname)
Robbert Krebbers's avatar
Robbert Krebbers committed
36
    (N : namespace) (φ: sts.state sts  iPropG Λ Σ) : iPropG Λ Σ :=
Ralf Jung's avatar
Ralf Jung committed
37 38
  inv N (sts_inv γ φ).

39 40 41 42
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
43 44

Section sts.
45
  Context `{stsG Λ Σ sts} (φ : sts.state sts  iPropG Λ Σ).
Ralf Jung's avatar
Ralf Jung committed
46 47 48
  Implicit Types N : namespace.
  Implicit Types P Q R : iPropG Λ Σ.
  Implicit Types γ : gname.
Robbert Krebbers's avatar
Robbert Krebbers committed
49 50 51 52 53
  Implicit Types S : sts.states sts.
  Implicit Types T : sts.tokens sts.

  (** Setoids *)
  Global Instance sts_inv_ne n γ :
54
    Proper (pointwise_relation _ (dist n) ==> dist n) (sts_inv γ).
Robbert Krebbers's avatar
Robbert Krebbers committed
55 56
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_inv; setoid_rewrite Hφ. Qed.
  Global Instance sts_inv_proper γ :
57
    Proper (pointwise_relation _ () ==> ()) (sts_inv γ).
Robbert Krebbers's avatar
Robbert Krebbers committed
58
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_inv; setoid_rewrite Hφ. Qed.
59
  Global Instance sts_ownS_proper γ : Proper (() ==> () ==> ()) (sts_ownS γ).
Ralf Jung's avatar
Ralf Jung committed
60
  Proof.
Ralf Jung's avatar
Ralf Jung committed
61
    intros S1 S2 HS T1 T2 HT. by rewrite !sts_ownS_eq /sts_ownS_def HS HT.
Ralf Jung's avatar
Ralf Jung committed
62
  Qed.
63
  Global Instance sts_own_proper γ s : Proper (() ==> ()) (sts_own γ s).
Ralf Jung's avatar
Ralf Jung committed
64
  Proof. intros T1 T2 HT. by rewrite !sts_own_eq /sts_own_def HT. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
65
  Global Instance sts_ctx_ne n γ N :
66
    Proper (pointwise_relation _ (dist n) ==> dist n) (sts_ctx γ N).
Robbert Krebbers's avatar
Robbert Krebbers committed
67 68
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_ctx Hφ. Qed.
  Global Instance sts_ctx_proper γ N :
69
    Proper (pointwise_relation _ () ==> ()) (sts_ctx γ N).
Robbert Krebbers's avatar
Robbert Krebbers committed
70
  Proof. by intros φ1 φ2 Hφ; rewrite /sts_ctx Hφ. Qed.
Ralf Jung's avatar
Ralf Jung committed
71

72 73
  (* The same rule as implication does *not* hold, as could be shown using
     sts_frag_included. *)
Ralf Jung's avatar
Ralf Jung committed
74
  Lemma sts_ownS_weaken E γ S1 S2 T1 T2 :
75
    T2  T1  S1  S2  sts.closed S2 T2 
76
    sts_ownS γ S1 T1  (|={E}=> sts_ownS γ S2 T2).
Ralf Jung's avatar
Ralf Jung committed
77
  Proof.
Ralf Jung's avatar
Ralf Jung committed
78
    intros ? ? ?. rewrite sts_ownS_eq. by apply own_update, sts_update_frag.
Ralf Jung's avatar
Ralf Jung committed
79
  Qed.
80

Ralf Jung's avatar
Ralf Jung committed
81
  Lemma sts_own_weaken E γ s S T1 T2 :
82
    T2  T1  s  S  sts.closed S T2 
83
    sts_own γ s T1  (|={E}=> sts_ownS γ S T2).
Ralf Jung's avatar
Ralf Jung committed
84
  Proof.
Ralf Jung's avatar
Ralf Jung committed
85
    intros ???. rewrite sts_ownS_eq sts_own_eq.
Ralf Jung's avatar
Ralf Jung committed
86 87
    by apply own_update, sts_update_frag_up.
  Qed.
88

Ralf Jung's avatar
Ralf Jung committed
89
  Lemma sts_ownS_op γ S1 S2 T1 T2 :
Ralf Jung's avatar
Ralf Jung committed
90
    T1  T2    sts.closed S1 T1  sts.closed S2 T2 
Ralf Jung's avatar
Ralf Jung committed
91
    sts_ownS γ (S1  S2) (T1  T2)  (sts_ownS γ S1 T1  sts_ownS γ S2 T2)%I.
Ralf Jung's avatar
Ralf Jung committed
92
  Proof. intros. by rewrite sts_ownS_eq /sts_ownS_def -own_op sts_op_frag. Qed.
Ralf Jung's avatar
Ralf Jung committed
93

94 95
  Lemma sts_alloc E N s :
    nclose N  E 
96
     φ s  (|={E}=>  γ, sts_ctx γ N φ  sts_own γ s (  sts.tok s)).
97
  Proof.
98
    intros HN. eapply sep_elim_True_r.
99
    { apply (own_alloc (sts_auth s (  sts.tok s)) N).
100
      apply sts_auth_valid; set_solver. }
101 102
    rewrite pvs_frame_l. rewrite -(pvs_mask_weaken N E) //.
    apply pvs_strip_pvs.
103
    rewrite sep_exist_l. apply exist_elim=>γ. rewrite -(exist_intro γ).
104
    trans ( sts_inv γ φ  sts_own γ s (  sts.tok s))%I.
105
    { rewrite /sts_inv -(exist_intro s) later_sep.
Ralf Jung's avatar
Ralf Jung committed
106
      ecancel [ φ _]%I. rewrite sts_own_eq.
107
      by rewrite -later_intro -own_op sts_op_auth_frag_up; last set_solver. }
Robbert Krebbers's avatar
Robbert Krebbers committed
108
    rewrite (inv_alloc N) /sts_ctx pvs_frame_r.
109 110 111
    by rewrite always_and_sep_l.
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
112
  Lemma sts_opened E γ S T :
113
    ( sts_inv γ φ  sts_ownS γ S T)
114
     (|={E}=>  s,  (s  S)   φ s  own γ (sts_auth s T)).
115
  Proof.
Ralf Jung's avatar
Ralf Jung committed
116
    rewrite /sts_inv sts_ownS_eq later_exist sep_exist_r. apply exist_elim=>s.
117 118 119
    rewrite later_sep pvs_timeless !pvs_frame_r. apply pvs_mono.
    rewrite -(exist_intro s).
    rewrite [(_  ▷φ _)%I]comm -!assoc -own_op -[(▷φ _  _)%I]comm.
120
    rewrite own_valid_l discrete_valid.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
    rewrite -!assoc. apply const_elim_sep_l=> Hvalid.
122
    assert (s  S) by eauto using sts_auth_frag_valid_inv.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
    rewrite const_equiv // left_id comm sts_op_auth_frag //.
124
    by assert ( sts_frag S T) as [??] by eauto using cmra_valid_op_r.
125 126
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
127
  Lemma sts_closing E γ s T s' T' :
128
    sts.steps (s, T) (s', T') 
129
    ( φ s'  own γ (sts_auth s T))  (|={E}=>  sts_inv γ φ  sts_own γ s' T').
130
  Proof.
Ralf Jung's avatar
Ralf Jung committed
131
    intros Hstep. rewrite /sts_inv sts_own_eq -(exist_intro s') later_sep.
Ralf Jung's avatar
Ralf Jung committed
132
    (* TODO it would be really nice to use cancel here *)
133
    rewrite [(_   φ _)%I]comm -assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
134
    rewrite -pvs_frame_l. apply sep_mono_r. rewrite -later_intro.
135
    rewrite own_valid_l discrete_valid. apply const_elim_sep_l=>Hval.
136
    trans (|={E}=> own γ (sts_auth s' T'))%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
137
    { by apply own_update, sts_update_auth. }
138
    by rewrite -own_op sts_op_auth_frag_up.
Ralf Jung's avatar
Ralf Jung committed
139
  Qed.
140

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

143
  Lemma sts_fsaS E N P (Ψ : V  iPropG Λ Σ) γ S T :
Ralf Jung's avatar
Ralf Jung committed
144
    fsaV  nclose N  E 
145 146
    P  sts_ctx γ N φ 
    P  (sts_ownS γ S T   s,
147
           (s  S)   φ s -
Ralf Jung's avatar
Ralf Jung committed
148
          fsa (E  nclose N) (λ x,  s' T',
149
             sts.steps (s, T) (s', T')   φ s' 
150 151
            (sts_own γ s' T' - Ψ x))) 
    P  fsa E Ψ.
152
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
153
    rewrite /sts_ctx=>? HN Hinv Hinner.
154 155
    eapply (inv_fsa fsa); eauto. rewrite Hinner=>{Hinner Hinv P HN}.
    apply wand_intro_l. rewrite assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
156
    rewrite (sts_opened (E  N)) !pvs_frame_r !sep_exist_r.
157 158
    apply (fsa_strip_pvs fsa). apply exist_elim=>s.
    rewrite (forall_elim s). rewrite [(_  _)%I]comm.
159 160 161
    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
162
    apply (fsa_mono_pvs fsa)=> x.
163
    rewrite sep_exist_l; apply exist_elim=> s'.
Ralf Jung's avatar
Ralf Jung committed
164 165
    rewrite sep_exist_l; apply exist_elim=>T'.
    rewrite comm -!assoc. apply const_elim_sep_l=>-Hstep.
166
    rewrite assoc [(_  (_ - _))%I]comm -assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
167
    rewrite (sts_closing (E  N)) //; [].
168 169 170 171
    rewrite pvs_frame_l. apply pvs_mono.
    by rewrite assoc [(_  _)%I]comm -assoc wand_elim_l.
  Qed.

172
  Lemma sts_fsa E N P (Ψ : V  iPropG Λ Σ) γ s0 T :
Ralf Jung's avatar
Ralf Jung committed
173
    fsaV  nclose N  E 
174 175
    P  sts_ctx γ N φ 
    P  (sts_own γ s0 T   s,
Robbert Krebbers's avatar
Robbert Krebbers committed
176
           (s  sts.up s0 T)   φ s -
Ralf Jung's avatar
Ralf Jung committed
177
          fsa (E  nclose N) (λ x,  s' T',
178
             (sts.steps (s, T) (s', T'))   φ s' 
179 180
            (sts_own γ s' T' - Ψ x))) 
    P  fsa E Ψ.
Ralf Jung's avatar
Ralf Jung committed
181
  Proof.
182
    rewrite sts_own_eq. intros. eapply sts_fsaS; try done; [].
Ralf Jung's avatar
Ralf Jung committed
183
    by rewrite sts_ownS_eq sts_own_eq. 
Ralf Jung's avatar
Ralf Jung committed
184
  Qed.
Ralf Jung's avatar
Ralf Jung committed
185
End sts.