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

6
(** The CMRA we need. *)
7 8
Class stsG Σ (sts : stsT) := StsG {
  sts_inG :> inG Σ (stsR sts);
Robbert Krebbers's avatar
Robbert Krebbers committed
9
  sts_inhabited :> Inhabited (sts.state sts);
Ralf Jung's avatar
Ralf Jung committed
10
}.
11
Coercion sts_inG : stsG >-> inG.
12
(** The Functor we need. *)
13
Definition stsGF (sts : stsT) : gFunctor := GFunctor (constRF (stsR sts)).
14
(* Show and register that they match. *)
15 16
Instance inGF_stsG sts `{inGF Λ (stsGF sts)}
  `{Inhabited (sts.state sts)} : stsG Λ sts.
17
Proof. split; try apply _. apply: inGF_inG. Qed.
18

19
Section definitions.
20 21 22
  Context `{irisG Λ Σ, stsG Σ sts} (γ : gname).

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

31 32 33 34 35 36
  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.
37
  Global Instance sts_ownS_proper : Proper (() ==> () ==> ()) sts_ownS.
38
  Proof. solve_proper. Qed.
39
  Global Instance sts_own_proper s : Proper (() ==> ()) (sts_own s).
40 41 42 43 44
  Proof. solve_proper. Qed.
  Global Instance sts_ctx_ne n N :
    Proper (pointwise_relation _ (dist n) ==> dist n) (sts_ctx N).
  Proof. solve_proper. Qed.
  Global Instance sts_ctx_proper N :
45
    Proper (pointwise_relation _ () ==> ()) (sts_ctx N).
46
  Proof. solve_proper. Qed.
47
  Global Instance sts_ctx_persistent N φ : PersistentP (sts_ctx N φ).
48 49
  Proof. apply _. Qed.
End definitions.
Robbert Krebbers's avatar
Robbert Krebbers committed
50

51
Typeclasses Opaque sts_own sts_ownS sts_ctx.
52 53 54 55
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
56 57

Section sts.
58
  Context `{irisG Λ Σ, stsG Σ sts} (φ : sts.state sts  iProp Σ).
Ralf Jung's avatar
Ralf Jung committed
59
  Implicit Types N : namespace.
60
  Implicit Types P Q R : iProp Σ.
Ralf Jung's avatar
Ralf Jung committed
61
  Implicit Types γ : gname.
Robbert Krebbers's avatar
Robbert Krebbers committed
62 63 64
  Implicit Types S : sts.states sts.
  Implicit Types T : sts.tokens sts.

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

72
  Lemma sts_own_weaken γ s S T1 T2 :
73
    T2  T1  s  S  sts.closed S T2 
74
    sts_own γ s T1 =r=> sts_ownS γ S T2.
75
  Proof. intros ???. by apply own_update, sts_update_frag_up. Qed.
76

Ralf Jung's avatar
Ralf Jung committed
77
  Lemma sts_ownS_op γ S1 S2 T1 T2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
78
    T1  T2  sts.closed S1 T1  sts.closed S2 T2 
79
    sts_ownS γ (S1  S2) (T1  T2)  (sts_ownS γ S1 T1  sts_ownS γ S2 T2).
80
  Proof. intros. by rewrite /sts_ownS -own_op sts_op_frag. Qed.
Ralf Jung's avatar
Ralf Jung committed
81

82
  Lemma sts_alloc E N s :
83
     φ s ={E}=>  γ, sts_ctx γ N φ  sts_own γ s (  sts.tok s).
84
  Proof.
85 86
    iIntros "Hφ". rewrite /sts_ctx /sts_own.
    iVs (own_alloc (sts_auth s (  sts.tok s))) as (γ) "Hγ".
87 88
    { apply sts_auth_valid; set_solver. }
    iExists γ; iRevert "Hγ"; rewrite -sts_op_auth_frag_up; iIntros "[Hγ $]".
89
    iVs (inv_alloc N _ (sts_inv γ φ) with "[Hφ Hγ]") as "#?"; auto.
90
    iNext. iExists s. by iFrame.
Ralf Jung's avatar
Ralf Jung committed
91
  Qed.
92

93 94 95 96 97
  Lemma sts_openS E N γ S T :
    nclose N  E 
    sts_ctx γ N φ  sts_ownS γ S T ={E,EN}=>  s,
       (s  S)   φ s   s' T',
       sts.steps (s, T) (s', T')   φ s' ={EN,E}= sts_own γ s' T'.
98
  Proof.
99 100
    iIntros (?) "[#? Hγf]". rewrite /sts_ctx /sts_ownS /sts_inv /sts_own.
    iInv N as (s) "[Hγ Hφ]" "Hclose". iTimeless "Hγ".
101
    iCombine "Hγ" "Hγf" as "Hγ"; iDestruct (own_valid with "#Hγ") as %Hvalid.
102 103
    assert (s  S) by eauto using sts_auth_frag_valid_inv.
    assert ( sts_frag S T) as [??] by eauto using cmra_valid_op_r.
104 105 106 107 108 109
    rewrite sts_op_auth_frag //.
    iVsIntro; iExists s; iSplit; [done|]; iFrame "Hφ".
    iIntros (s' T') "[% Hφ]".
    iVs (own_update with "Hγ") as "Hγ"; first eauto using sts_update_auth.
    iRevert "Hγ"; rewrite -sts_op_auth_frag_up; iIntros "[Hγ $]".
    iApply "Hclose". iNext; iExists s'; by iFrame.
110 111
  Qed.

112 113 114 115 116 117
  Lemma sts_open E N γ s0 T :
    nclose N  E 
    sts_ctx γ N φ  sts_own γ s0 T ={E,EN}=>  s,
       (s  sts.up s0 T)   φ s   s' T',
       (sts.steps (s, T) (s', T'))   φ s' ={EN,E}= sts_own γ s' T'.
  Proof. by apply sts_openS. Qed.
Ralf Jung's avatar
Ralf Jung committed
118
End sts.