adequacy.v 8.29 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
Set Default Proof Using "Type".
7
Import uPred.
Robbert Krebbers's avatar
Robbert Krebbers committed
8

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

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 Σ.
22
Proof. solve_inG. Qed.
23 24

(* Allocation *)
25
Lemma wsat_alloc `{invPreG Σ} : (|==>  _ : invG Σ, wsat  ownE )%I.
26 27 28 29 30 31
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).
Robbert Krebbers's avatar
Robbert Krebbers committed
32
  rewrite /wsat /ownE -lock; iFrame.
33
  iExists . rewrite fmap_empty big_opM_empty. by iFrame.
34 35 36
Qed.

(* Program logic adequacy *)
37
Record adequate {Λ} (s : stuckness) (e1 : expr Λ) (σ1 : state Λ) (φ : val Λ  Prop) := {
38 39 40
  adequate_result t2 σ2 v2 :
   rtc step ([e1], σ1) (of_val v2 :: t2, σ2)  φ v2;
  adequate_safe t2 σ2 e2 :
Ralf Jung's avatar
Ralf Jung committed
41
   s = NotStuck 
42
   rtc step ([e1], σ1) (t2, σ2) 
43
   e2  t2  (is_Some (to_val e2)  reducible e2 σ2)
44 45 46
}.

Theorem adequate_tp_safe {Λ} (e1 : expr Λ) t2 σ1 σ2 φ :
Ralf Jung's avatar
Ralf Jung committed
47
  adequate NotStuck e1 σ1 φ 
48 49 50 51 52 53
  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).
Ralf Jung's avatar
Ralf Jung committed
54
  destruct (adequate_safe NotStuck 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 69
Notation world' E σ := (wsat  ownE E  state_interp σ)%I (only parsing).
Notation world σ := (world'  σ) (only parsing).
70

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

73
Lemma wp_step s E e1 σ1 e2 σ2 efs Φ :
74
  prim_step e1 σ1 e2 σ2 efs 
75 76
  world' E σ1  WP e1 @ s; E {{ Φ }}
  ==  |==>  (world' E σ2  WP e2 @ s; E {{ Φ }}  wptp s efs).
77
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
78 79
  rewrite {1}wp_unfold /wp_pre. iIntros (?) "[(Hw & HE & Hσ) H]".
  rewrite (val_stuck e1 σ1 e2 σ2 efs) // fupd_eq /fupd_def.
80 81
  iMod ("H" $! σ1 with "Hσ [Hw HE]") as ">(Hw & HE & _ & H)"; first by iFrame.
  iModIntro; iNext.
Robbert Krebbers's avatar
Robbert Krebbers committed
82
  iMod ("H" $! e2 σ2 efs with "[%] [$Hw $HE]") as ">($ & $ & $ & $)"; auto.
83 84
Qed.

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

99
Lemma wptp_steps s n e1 t1 t2 σ1 σ2 Φ :
100
  nsteps step n (e1 :: t1, σ1) (t2, σ2) 
101
  world σ1  WP e1 @ s;  {{ Φ }}  wptp s t1 
102
  Nat.iter (S n) (λ P, |==>  P) ( e2 t2',
103
    t2 = e2 :: t2'  world σ2  WP e2 @ s;  {{ Φ }}  wptp s 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 113 114 115 116
Lemma bupd_iter_laterN_mono n P Q `{!Plain Q} :
  (P  Q)  Nat.iter n (λ P, |==>  P)%I P  ^n Q.
Proof.
  intros HPQ. induction n as [|n IH]=> //=. by rewrite IH bupd_plain.
Qed.
117

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

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

135
Lemma wp_safe E e σ Φ :
136
  world' E σ - WP e @ E {{ Φ }} ==  is_Some (to_val e)  reducible e σ⌝.
137
Proof.
138
  rewrite wp_unfold /wp_pre. iIntros "(Hw&HE&Hσ) H".
139 140 141 142
  destruct (to_val e) as [v|] eqn:?.
  { iIntros "!> !> !%". left. by exists v. }
  rewrite fupd_eq. iMod ("H" with "Hσ [-]") as ">(?&?&%&?)"; first by iFrame.
  iIntros "!> !> !%". by right.
143
Qed.
Ralf Jung's avatar
Ralf Jung committed
144

145 146
Lemma wptp_safe n e1 e2 t1 t2 σ1 σ2 Φ :
  nsteps step n (e1 :: t1, σ1) (t2, σ2)  e2  t2 
Ralf Jung's avatar
Ralf Jung committed
147
  world σ1  WP e1 {{ Φ }}  wptp NotStuck t1
148
   ^(S (S n)) is_Some (to_val e2)  reducible e2 σ2.
Robbert Krebbers's avatar
Robbert Krebbers committed
149
Proof.
150
  intros ? He2. rewrite wptp_steps // laterN_later. apply: bupd_iter_laterN_mono.
151
  iDestruct 1 as (e2' t2' ?) "(Hw & H & Htp)"; simplify_eq.
152 153 154
  apply elem_of_cons in He2 as [<-|?].
  - iMod (wp_safe with "Hw H") as "$".
  - iMod (wp_safe with "Hw [Htp]") as "$". by iApply (big_sepL_elem_of with "Htp").
Robbert Krebbers's avatar
Robbert Krebbers committed
155
Qed.
156

157
Lemma wptp_invariance s n e1 e2 t1 t2 σ1 σ2 φ Φ :
158
  nsteps step n (e1 :: t1, σ1) (t2, σ2) 
159
  (state_interp σ2 ={,}= ⌜φ⌝)  world σ1  WP e1 @ s;  {{ Φ }}  wptp s t1
160
   ^(S (S n)) ⌜φ⌝.
161
Proof.
162 163 164
  intros ?. rewrite wptp_steps // bupd_iter_frame_l laterN_later.
  apply: bupd_iter_laterN_mono.
  iIntros "[Hback H]"; iDestruct "H" as (e2' t2' ->) "[(Hw&HE&Hσ) _]".
165
  rewrite fupd_eq.
Robbert Krebbers's avatar
Robbert Krebbers committed
166
  iMod ("Hback" with "Hσ [$Hw $HE]") as "> (_ & _ & $)"; auto.
167
Qed.
168
End adequacy.
Ralf Jung's avatar
Ralf Jung committed
169

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

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