adequacy.v 8.45 KB
Newer Older
1
From iris.program_logic Require Export weakestpre.
2 3
From iris.algebra Require Import gmap auth agree gset coPset.
From iris.base_logic Require Import big_op soundness.
4
From iris.base_logic.lib Require Import wsat.
5
From iris.proofmode Require Import tactics.
6
Import uPred.
Robbert Krebbers's avatar
Robbert Krebbers committed
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
(* Global functor setup *)
Definition invΣ : gFunctors :=
  #[GFunctor (authRF (gmapURF positive (agreeRF (laterCF idCF))));
    GFunctor (constRF coPset_disjUR);
    GFunctor (constRF (gset_disjUR positive))].

Class invPreG (Σ : gFunctors) : Set := WsatPreG {
  inv_inPreG :> inG Σ (authR (gmapUR positive (agreeR (laterC (iPreProp Σ)))));
  enabled_inPreG :> inG Σ coPset_disjR;
  disabled_inPreG :> inG Σ (gset_disjR positive);
}.

Instance subG_invΣ {Σ} : subG invΣ Σ  invPreG Σ.
Proof.
  intros [?%subG_inG [?%subG_inG ?%subG_inG]%subG_inv]%subG_inv; by constructor.
Qed.

(* Allocation *)
26
Lemma wsat_alloc `{invPreG Σ} : (|==>  _ : invG Σ, wsat  ownE )%I.
27 28 29 30 31 32 33 34 35 36 37
Proof.
  iIntros.
  iMod (own_alloc ( ( : gmap _ _))) as (γI) "HI"; first done.
  iMod (own_alloc (CoPset )) as (γE) "HE"; first done.
  iMod (own_alloc (GSet )) as (γD) "HD"; first done.
  iModIntro; iExists (WsatG _ _ _ _ γI γE γD).
  rewrite /wsat /ownE; iFrame.
  iExists . rewrite fmap_empty big_sepM_empty. by iFrame.
Qed.

(* Program logic adequacy *)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
Record adequate {Λ} (e1 : expr Λ) (σ1 : state Λ) (φ : val Λ  Prop) := {
  adequate_result t2 σ2 v2 :
   rtc step ([e1], σ1) (of_val v2 :: t2, σ2)  φ v2;
  adequate_safe t2 σ2 e2 :
   rtc step ([e1], σ1) (t2, σ2) 
   e2  t2  (is_Some (to_val e2)  reducible e2 σ2)
}.

Theorem adequate_tp_safe {Λ} (e1 : expr Λ) t2 σ1 σ2 φ :
  adequate e1 σ1 φ 
  rtc step ([e1], σ1) (t2, σ2) 
  Forall (λ e, is_Some (to_val e)) t2   t3 σ3, step (t2, σ2) (t3, σ3).
Proof.
  intros Had ?.
  destruct (decide (Forall (λ e, is_Some (to_val e)) t2)) as [|Ht2]; [by left|].
  apply (not_Forall_Exists _), Exists_exists in Ht2; destruct Ht2 as (e2&?&He2).
54
  destruct (adequate_safe e1 σ1 φ Had t2 σ2 e2) as [?|(e3&σ3&efs&?)];
55 56 57
    rewrite ?eq_None_not_Some; auto.
  { exfalso. eauto. }
  destruct (elem_of_list_split t2 e2) as (t2'&t2''&->); auto.
58
  right; exists (t2' ++ e3 :: t2'' ++ efs), σ3; econstructor; eauto.
59 60
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
61
Section adequacy.
62
Context `{irisG Λ Σ}.
63
Implicit Types e : expr Λ.
64 65 66
Implicit Types P Q : iProp Σ.
Implicit Types Φ : val Λ  iProp Σ.
Implicit Types Φs : list (val Λ  iProp Σ).
Robbert Krebbers's avatar
Robbert Krebbers committed
67

68
Notation world σ := (wsat  ownE   state_interp σ)%I.
69

70
Notation wptp t := ([ list] ef  t, WP ef {{ _, True }})%I.
71

72 73
Lemma wp_step e1 σ1 e2 σ2 efs Φ :
  prim_step e1 σ1 e2 σ2 efs 
74
  world σ1  WP e1 {{ Φ }} ==  |==>  (world σ2  WP e2 {{ Φ }}  wptp efs).
75 76 77
Proof.
  rewrite {1}wp_unfold /wp_pre. iIntros (Hstep) "[(Hw & HE & Hσ) [H|[_ H]]]".
  { iDestruct "H" as (v) "[% _]". apply val_stuck in Hstep; simplify_eq. }
78
  rewrite fupd_eq /fupd_def.
79 80
  iMod ("H" $! σ1 with "Hσ [Hw HE]") as ">(Hw & HE & _ & H)"; first by iFrame.
  iModIntro; iNext.
81
  by iMod ("H" $! e2 σ2 efs with "[%] [$Hw $HE]") as ">($ & $ & $ & $)".
82 83 84 85
Qed.

Lemma wptp_step e1 t1 t2 σ1 σ2 Φ :
  step (e1 :: t1,σ1) (t2, σ2) 
86
  world σ1  WP e1 {{ Φ }}  wptp t1
Ralf Jung's avatar
Ralf Jung committed
87
  ==  e2 t2', t2 = e2 :: t2'   |==>  (world σ2  WP e2 {{ Φ }}  wptp t2').
88 89
Proof.
  iIntros (Hstep) "(HW & He & Ht)".
90 91
  destruct Hstep as [e1' σ1' e2' σ2' efs [|? t1'] t2' ?? Hstep]; simplify_eq/=.
  - iExists e2', (t2' ++ efs); iSplitR; first eauto.
92
    rewrite big_sepL_app. iFrame "Ht". iApply wp_step; try iFrame; eauto.
93
  - iExists e, (t1' ++ e2' :: t2' ++ efs); iSplitR; first eauto.
94
    rewrite !big_sepL_app !big_sepL_cons big_sepL_app.
95 96 97 98 99 100
    iDestruct "Ht" as "($ & He' & $)"; iFrame "He".
    iApply wp_step; try iFrame; eauto.
Qed.

Lemma wptp_steps n e1 t1 t2 σ1 σ2 Φ :
  nsteps step n (e1 :: t1, σ1) (t2, σ2) 
101
  world σ1  WP e1 {{ Φ }}  wptp t1 
102
  Nat.iter (S n) (λ P, |==>  P) ( e2 t2',
Ralf Jung's avatar
Ralf Jung committed
103
    t2 = e2 :: t2'  world σ2  WP e2 {{ Φ }}  wptp t2').
Robbert Krebbers's avatar
Robbert Krebbers committed
104
Proof.
105 106 107
  revert e1 t1 t2 σ1 σ2; simpl; induction n as [|n IH]=> e1 t1 t2 σ1 σ2 /=.
  { inversion_clear 1; iIntros "?"; eauto 10. }
  iIntros (Hsteps) "H". inversion_clear Hsteps as [|?? [t1' σ1']].
108 109
  iMod (wptp_step with "H") as (e1' t1'') "[% H]"; first eauto; simplify_eq.
  iModIntro; iNext; iMod "H" as ">?". by iApply IH.
Robbert Krebbers's avatar
Robbert Krebbers committed
110
Qed.
111

112
Instance bupd_iter_mono n : Proper (() ==> ()) (Nat.iter n (λ P, |==>  P)%I).
113 114
Proof. intros P Q HP. induction n; simpl; do 2?f_equiv; auto. Qed.

115
Lemma bupd_iter_frame_l n R Q :
116
  R  Nat.iter n (λ P, |==>  P) Q  Nat.iter n (λ P, |==>  P) (R  Q).
117 118
Proof.
  induction n as [|n IH]; simpl; [done|].
119
  by rewrite bupd_frame_l {1}(later_intro R) -later_sep IH.
120 121
Qed.

122 123
Lemma wptp_result n e1 t1 v2 t2 σ1 σ2 φ :
  nsteps step n (e1 :: t1, σ1) (of_val v2 :: t2, σ2) 
Ralf Jung's avatar
Ralf Jung committed
124 125
  world σ1  WP e1 {{ v, ⌜φ v }}  wptp t1 
  Nat.iter (S (S n)) (λ P, |==>  P) ⌜φ v2.
Robbert Krebbers's avatar
Robbert Krebbers committed
126
Proof.
Ralf Jung's avatar
Ralf Jung committed
127
  intros. rewrite wptp_steps //.
128
  rewrite (Nat_iter_S_r (S n)). apply bupd_iter_mono.
129
  iDestruct 1 as (e2 t2') "(% & (Hw & HE & _) & H & _)"; simplify_eq.
130
  iDestruct (wp_value_inv with "H") as "H". rewrite fupd_eq /fupd_def.
131
  iMod ("H" with "[Hw HE]") as ">(_ & _ & $)"; iFrame; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
132
Qed.
Ralf Jung's avatar
Ralf Jung committed
133

134
Lemma wp_safe e σ Φ :
Ralf Jung's avatar
Ralf Jung committed
135
  world σ  WP e {{ Φ }} ==  is_Some (to_val e)  reducible e σ⌝.
136
Proof.
137 138
  rewrite wp_unfold /wp_pre. iIntros "[(Hw&HE&Hσ) [H|[_ H]]]".
  { iDestruct "H" as (v) "[% _]"; eauto 10. }
139
  rewrite fupd_eq. iMod ("H" with "* Hσ [-]") as ">(?&?&%&?)"; first by iFrame.
140
  eauto 10.
141
Qed.
Ralf Jung's avatar
Ralf Jung committed
142

143 144
Lemma wptp_safe n e1 e2 t1 t2 σ1 σ2 Φ :
  nsteps step n (e1 :: t1, σ1) (t2, σ2)  e2  t2 
145
  world σ1  WP e1 {{ Φ }}  wptp t1 
Ralf Jung's avatar
Ralf Jung committed
146
  Nat.iter (S (S n)) (λ P, |==>  P) is_Some (to_val e2)  reducible e2 σ2.
Robbert Krebbers's avatar
Robbert Krebbers committed
147
Proof.
148
  intros ? He2. rewrite wptp_steps //; rewrite (Nat_iter_S_r (S n)). apply bupd_iter_mono.
149 150
  iDestruct 1 as (e2' t2') "(% & Hw & H & Htp)"; simplify_eq.
  apply elem_of_cons in He2 as [<-|?]; first (iApply wp_safe; by iFrame "Hw H").
151
  iApply wp_safe. iFrame "Hw". by iApply (big_sepL_elem_of with "Htp").
Robbert Krebbers's avatar
Robbert Krebbers committed
152
Qed.
153

Robbert Krebbers's avatar
Robbert Krebbers committed
154
Lemma wptp_invariance n e1 e2 t1 t2 σ1 σ2 φ Φ :
155
  nsteps step n (e1 :: t1, σ1) (t2, σ2) 
Robbert Krebbers's avatar
Robbert Krebbers committed
156
  (state_interp σ2 ={,}= ⌜φ⌝)  world σ1  WP e1 {{ Φ }}  wptp t1
157
   Nat.iter (S (S n)) (λ P, |==>  P) ⌜φ⌝.
158
Proof.
159 160
  intros ?. rewrite wptp_steps //.
  rewrite (Nat_iter_S_r (S n)) !bupd_iter_frame_l. apply bupd_iter_mono.
Robbert Krebbers's avatar
Robbert Krebbers committed
161
  iIntros "[Hback H]".
162
  iDestruct "H" as (e2' t2') "(% & (Hw&HE&Hσ) & _)"; subst.
163
  rewrite fupd_eq.
Robbert Krebbers's avatar
Robbert Krebbers committed
164
  iMod ("Hback" with "Hσ [$Hw $HE]") as "> (_ & _ & $)"; auto.
165
Qed.
166
End adequacy.
Ralf Jung's avatar
Ralf Jung committed
167

168
Theorem wp_adequacy Σ Λ `{invPreG Σ} e σ φ :
169 170 171 172
  ( `{Hinv : invG Σ},
     True ={}=  stateI : state Λ  iProp Σ,
       let _ : irisG Λ Σ := IrisG _ _ Hinv stateI in
       stateI σ  WP e {{ v, ⌜φ v }}) 
173
  adequate e σ φ.
Ralf Jung's avatar
Ralf Jung committed
174
Proof.
175 176
  intros Hwp; split.
  - intros t2 σ2 v2 [n ?]%rtc_nsteps.
177
    eapply (soundness (M:=iResUR Σ) _ (S (S (S n)))); iIntros "".
178 179 180 181 182
    rewrite Nat_iter_S. iMod wsat_alloc as (Hinv) "[Hw HE]".
    rewrite fupd_eq in Hwp; iMod (Hwp with "[$Hw $HE]") as ">(Hw & HE & Hwp)".
    iDestruct "Hwp" as (Istate) "[HI Hwp]".
    iModIntro. iNext. iApply (@wptp_result _ _ (IrisG _ _ Hinv Istate)); eauto.
    iFrame. by iApply big_sepL_nil.
183
  - intros t2 σ2 e2 [n ?]%rtc_nsteps ?.
184
    eapply (soundness (M:=iResUR Σ) _ (S (S (S n)))); iIntros "".
185 186 187 188 189
    rewrite Nat_iter_S. iMod wsat_alloc as (Hinv) "[Hw HE]".
    rewrite fupd_eq in Hwp; iMod (Hwp with "[$Hw $HE]") as ">(Hw & HE & Hwp)".
    iDestruct "Hwp" as (Istate) "[HI Hwp]".
    iModIntro. iNext. iApply (@wptp_safe _ _ (IrisG _ _ Hinv Istate)); eauto.
    iFrame. by iApply big_sepL_nil.
Robbert Krebbers's avatar
Robbert Krebbers committed
190
Qed.
191

192
Theorem wp_invariance Σ Λ `{invPreG Σ} e σ1 t2 σ2 φ Φ :
193 194 195
  ( `{Hinv : invG Σ},
     True ={}=  stateI : state Λ  iProp Σ,
       let _ : irisG Λ Σ := IrisG _ _ Hinv stateI in
Robbert Krebbers's avatar
Robbert Krebbers committed
196
       stateI σ1  WP e {{ Φ }}  (stateI σ2 ={,}= ⌜φ⌝)) 
197
  rtc step ([e], σ1) (t2, σ2) 
198
  φ.
199
Proof.
200
  intros Hwp [n ?]%rtc_nsteps.
201
  eapply (soundness (M:=iResUR Σ) _ (S (S (S n)))); iIntros "".
202 203
  rewrite Nat_iter_S. iMod wsat_alloc as (Hinv) "[Hw HE]".
  rewrite {1}fupd_eq in Hwp; iMod (Hwp with "[$Hw $HE]") as ">(Hw & HE & Hwp)".
Robbert Krebbers's avatar
Robbert Krebbers committed
204 205 206
  iDestruct "Hwp" as (Istate) "(HIstate & Hwp & Hclose)".
  iModIntro. iNext. iApply (@wptp_invariance _ _ (IrisG _ _ Hinv Istate)); eauto.
  iFrame "Hw HE Hwp HIstate Hclose". by iApply big_sepL_nil.
207
Qed.