TimeReceipts.v 12.4 KB
Newer Older
Glen Mével's avatar
Glen Mével committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
From iris.heap_lang Require Import proofmode notation adequacy.
From iris.algebra Require Import auth.
From iris.base_logic Require Import invariants.
From iris.proofmode Require Import classes.
From stdpp Require Import namespaces.

Require Import Auth_nat Auth_mnat Misc Reduction Tactics.
Require Export Translation Simulation.

(* Require Import TimeCredits. *)

Implicit Type e : expr.
Implicit Type v : val.
Implicit Type σ : state.
Implicit Type t : list expr.
Implicit Type K : ectx heap_ectx_lang.
Implicit Type m n : nat.

(* rem: Unicode notations?
 *   medals: 🏅🥇🎖
 *   gears: ⚙⛭
 *   shields: ⛨
 *   florettes: ✿❀
 *   squares: ▣ ▢ ▤ ▥ ☑
 *   circles: ◉ ◎ ◌ ◍ ☉
 *   pentagons: ⬟ ⬠
 *   hexagons: ⬢ ⬡
 *   shogi pieces: ☗ ☖
29
 *   sandglasses: ⧗ ⧖
Glen Mével's avatar
Glen Mével committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
 *   other: ⮝ ⮙ ⯊ ⯎
 *)



(*
 * Prerequisites on the global monoid Σ
 *)

Class timeReceiptHeapPreG Σ := {
  timeReceiptHeapPreG_heapPreG :> heapPreG Σ ;
  timeReceiptHeapPreG_inG1 :> inG Σ (authR natUR) ;
  timeReceiptHeapPreG_inG2 :> inG Σ (authR mnatUR) ;
}.

Class timeReceiptLoc := {
  timeReceiptLoc_loc : loc ;
}.

Class timeReceiptHeapG Σ := {
  timeReceiptHeapG_heapG :> heapG Σ ;
  timeReceiptHeapG_inG1 :> inG Σ (authR natUR) ;
  timeReceiptHeapG_inG2 :> inG Σ (authR mnatUR) ;
  timeReceiptHeapG_loc :> timeReceiptLoc ;
  timeReceiptHeapG_name1 : gname ;
  timeReceiptHeapG_name2 : gname ;
}.

Local Notation γ1 := timeReceiptHeapG_name1.
Local Notation γ2 := timeReceiptHeapG_name2.
Local Notation  := timeReceiptLoc_loc.



(*
 * Implementation and specification of `TR` and `tock`
 *)

Section Tock.

  Context `{timeReceiptLoc}.

  Definition loop : val :=
    rec: "f" <> := "f" #().

  Definition tock : val :=
    tick loop .

End Tock.



Section TockSpec.

  Context `{timeReceiptHeapG Σ}.

  Definition TR (n : nat) : iProp Σ :=
    own γ1 (nat n).

  Definition TRdup (n : mnat) : iProp Σ :=
    own γ2 (mnat n).
  Arguments TRdup _%nat_scope.

  Lemma TR_plus m n :
    TR (m + n)  (TR m  TR n)%I.
  Proof. by rewrite /TR auth_frag_op own_op. Qed.
  Lemma TR_succ n :
    TR (S n)  (TR 1  TR n)%I.
  Proof. by rewrite (eq_refl : S n = 1 + n)%nat TR_plus. Qed.

  Lemma TR_timeless n :
    Timeless (TR n).
  Proof. exact _. Qed.

  (* note: IntoAnd false will become IntoSep in a future version of Iris *)
  Global Instance into_sep_TR_plus m n p : IntoAnd p (TR (m + n)) (TR m) (TR n).
  Proof. rewrite /IntoAnd TR_plus ; iIntros "[Hm Hn]". destruct p ; iFrame. Qed.
  Global Instance from_sep_TR_plus m n : FromAnd false (TR (m + n)) (TR m) (TR n).
  Proof. by rewrite /FromAnd TR_plus. Qed.
  Global Instance into_sep_TR_succ n p : IntoAnd p (TR (S n)) (TR 1) (TR n).
  Proof. rewrite /IntoAnd TR_succ ; iIntros "[H1 Hn]". destruct p ; iFrame. Qed.
  Global Instance from_sep_TR_succ n : FromAnd false (TR (S n)) (TR 1) (TR n).
  Proof. by rewrite /FromAnd [TR (S n)] TR_succ. Qed.

  Lemma TRdup_max m n :
    TRdup (m `max` n)  (TRdup m  TRdup n)%I.
  Proof. by rewrite /TRdup auth_frag_op own_op. Qed.

  Lemma TRdup_timeless n :
    Timeless (TRdup n).
  Proof. exact _. Qed.

  (* note: IntoAnd false will become IntoSep in a future version of Iris *)
  Global Instance into_sep_TRdup_max m n p : IntoAnd p (TRdup (m `max` n)) (TRdup m) (TRdup n).
  Proof. rewrite /IntoAnd TRdup_max ; iIntros "[Hm Hn]". destruct p ; iFrame. Qed.
  Global Instance from_sep_TRdup_max m n : FromAnd false (TRdup (m `max` n)) (TRdup m) (TRdup n).
  Proof. by rewrite /FromAnd TRdup_max. Qed.

  Definition timeReceiptN := nroot .@ "timeReceipt".

  Definition TOCKCTXT (nmax : nat) : iProp Σ :=
    inv timeReceiptN ( (n:nat),   #(nmax-n-1)  own γ1 (nat n)  own γ2 (mnat n)  (n < nmax)%nat)%I.

  Lemma TR_nmax_absurd (nmax : nat) (E : coPset) :
    timeReceiptN  E 
    TOCKCTXT nmax - TR nmax ={E}= False.
  Proof.
    iIntros (?) "#Inv Hγ1◯".
    iInv timeReceiptN as (n) ">(Hℓ & Hγ1● & Hγ2● & In)" "InvClose" ; iDestruct "In" as %In.
    iDestruct (own_auth_nat_le with "Hγ1● Hγ1◯") as %In'.
    exfalso ; lia.
  Qed.

  Lemma TR_TRdup (nmax : nat) (E : coPset) (n : nat) :
    timeReceiptN  E 
    TOCKCTXT nmax - TR n ={E}= TR n  TRdup n.
  Proof.
    iIntros (?) "#Inv Hγ1◯".
    iInv timeReceiptN as (m) ">(Hℓ & Hγ1● & Hγ2● & Im)" "InvClose".
    iDestruct (own_auth_nat_le with "Hγ1● Hγ1◯") as %In.
    iDestruct (auth_mnat_update_read_auth with "Hγ2●") as ">[Hγ2● Hγ2◯]".
    iAssert (TR n  TRdup n)%I with "[$Hγ1◯ Hγ2◯]" as "$". {
      rewrite (_ : m = m `max` n) ; last lia.
      iDestruct "Hγ2◯" as "[_ $]".
    }
    iApply "InvClose". auto with iFrame.
  Qed.

  Theorem loop_spec s E (Φ : val  iProp Σ) :
    WP loop #() @ s ; E {{ Φ }}%I.
  Proof.
    iLöb as "IH". wp_rec. iExact "IH".
  Qed.

  Theorem tock_spec (nmax : nat) s E e v :
    timeReceiptN  E 
    IntoVal e v 
    TOCKCTXT nmax -
    {{{ True }}} tock e @ s ; E {{{ RET v ; TR 1 }}}.
  Proof.
    intros ? <- % of_to_val. iIntros "#Inv" (Ψ) "!# _ HΨ".
    iLöb as "IH".
    wp_lam.
    (* open the invariant, in order to read the value k = nmax−n−1 of location ℓ: *)
    wp_bind (! _)%E ;
    iInv timeReceiptN as (n) ">(Hℓ & Hγ1● & Hγ2● & In)" "InvClose" ; iDestruct "In" as %In.
    wp_load.
    (* close the invariant: *)
    iMod ("InvClose" with "[ Hℓ Hγ1● Hγ2● ]") as "_" ; [ by auto with iFrame | iModIntro ].
    wp_let.
    (* test whether k ≤ 0: *)
    wp_op ; destruct (bool_decide (nmax - n - 1  0)) eqn:Im ; wp_if.
    (* — if k ≤ 0 then we loop indefinitely, which gives us any spec we want
         (because we are reasoning in partial correctness): *)
    - iApply loop_spec.
    (* — otherwise: *)
    - apply Is_true_false in Im ; rewrite -> bool_decide_spec in Im.
      wp_op.
      (* open the invariant again, in order to perform CAS on location ℓ: *)
      wp_bind (CAS _ _ _)%E ;
      iInv timeReceiptN as (n') ">(Hℓ & Hγ1● & Hγ2● & In')" "InvClose" ; iDestruct "In'" as %In'.
      (* test whether the value ℓ is still k, by comparing n with n': *)
      destruct (nat_eq_dec n n') as [ <- | Hneq ].
      (* — if it holds, then CAS succeeds and ℓ is decremented: *)
      + wp_cas_suc.
        (* reform the invariant with n+1 instead of n, and an additional time
           receipt produced: *)
        replace (nmax - n - 1 - 1) with (nmax - (n+1)%nat - 1) by lia.
        iMod (auth_nat_update_incr _ _ 1 with "Hγ1●") as "[Hγ1● Hγ1◯]" ; simpl.
        iMod (auth_mnat_update_incr _ _ 1 with "Hγ2●") as "Hγ2●" ; simpl.
        assert ((n+1) < nmax)%nat by lia.
        (* close the invariant: *)
        iMod ("InvClose" with "[ Hℓ Hγ1● Hγ2● ]") as "_" ; [ by auto with iFrame | iModIntro ].
        (* finish: *)
        wp_if. iApply "HΨ" ; iExact "Hγ1◯".
      (* — otherwise, CAS fails and ℓ is unchanged: *)
      + wp_cas_fail ; first (injection ; lia).
        (* close the invariant as is: *)
        iMod ("InvClose" with "[ Hℓ Hγ1● Hγ2● ]") as "_" ; [ by auto with iFrame | iModIntro ] ; clear dependent n.
        wp_if.
        (* conclude using the induction hypothesis: *)
        iApply ("IH" with "HΨ").
  Qed.

  Theorem tock_spec_simple (nmax : nat) v :
    TOCKCTXT nmax -
    {{{ True }}} tock v {{{ RET v ; TR 1 }}}.
  Proof.
    iIntros "#Inv" (Ψ) "!# H HΨ".
    by iApply (tock_spec with "Inv H HΨ").
  Qed.

End TockSpec.



(*
 * Simulation
 *)

Notation trtranslation := (translation tock).
Notation trtranslationV := (translationV tock).
Notation trtranslationS := (translationS tock).
Notation trtranslationKi := (translationKi tock).
Notation trtranslationK := (translationK tock).

Notation "E« e »" := (trtranslation e%E).
Notation "V« v »" := (trtranslationV v%V).
Notation "Ki« ki »" := (trtranslationKi ki).
Notation "K« K »" := (trtranslationK K).
Notation "S« σ »" := (trtranslationS σ%V).
Notation "S« σ , n »" := (<[ := LitV (LitInt n%nat)]> (trtranslationS σ%V)).
Notation "T« t »" := (trtranslation <$> t%E).

Notation "« e »" := (trtranslation e%E).
Notation "« e »" := (trtranslation e%E) : expr_scope.
Notation "« v »" := (trtranslationV v%V) : val_scope.

(* for some reason, these notations make parsing fail,
 * even if they only regard printing… *)
(*
Notation "« e »" := (trtranslation e%E) (only printing).
Notation "« v »" := (trtranslationV v%V) (only printing).
Notation "« ki »" := (trtranslationKi ki) (only printing).
Notation "« K »" := (trtranslationK K) (only printing).
Notation "« σ »" := (trtranslationS σ%V) (only printing).
Notation "« σ , n »" := (<[ℓ := LitV (LitInt n%nat)]> (trtranslationS σ%V)) (only printing).
Notation "« t »" := (trtranslation <$> t%E) (only printing).
*)



Section Soundness.

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  Lemma adequate_trtranslation__adequate m (φ : val  Prop) e σ :
    is_closed [] e 
    ( `{timeReceiptLoc}, adequate NotStuck «e» S«σ, m» (φ  invtranslationV)) 
    adequate_n NotStuck m e σ φ.
  Proof.
    intros.
    apply (adequate_translation__adequate (λ 1, @tock {| timeReceiptLoc_loc := 1 |})).
    - intro 1.
      rewrite (_ : 1 = @timeReceiptLoc_loc {| timeReceiptLoc_loc := 1 |}) ; last done.
      apply exec_tick_success.
    - done.
    - intro 1.
      rewrite (_ : 1 = @timeReceiptLoc_loc {| timeReceiptLoc_loc := 1 |}) ; last done.
      done.
  Qed.

  (* derive the adequacy of the translated program from a Hoare triple in Iris. *)

  Lemma auth_mnat_alloc `{inG Σ (authR mnatUR)} (n : mnat) :
    (|==>  γ, own γ (mnat n)  own γ (mnat n))%I.
  Proof.
    by iMod (own_alloc (mnat n  mnat n)) as (γ) "[? ?]" ; auto with iFrame.
  Qed.
  Global Arguments auth_mnat_alloc {_ _} n%nat.

  Lemma spec_trtranslation__adequate_translation {Σ} (nmax : nat) (ψ : val  Prop) e :
    (0 < nmax)%nat 
    ( `{!timeReceiptHeapG Σ},
      TOCKCTXT nmax -
      {{{ True }}} «e» {{{ v, RET v ; ⌜ψ v }}}
    ) 
     `{!timeReceiptHeapPreG Σ} `{!timeReceiptLoc} σ, adequate NotStuck «e» S«σ,nmax-1» ψ.
  Proof.
    intros Inmax Hspec HpreG Hloc σ.
    (* apply the adequacy results. *)
    apply (wp_adequacy _ _) ; simpl ; intros HinvG.
    (* … now we have to prove a WP. *)
    set σ' := S«σ».
    (* allocate the heap, including cell ℓ (on which we need to keep an eye): *)
    iMod (own_alloc ( to_gen_heap (<[ := #(nmax-1)%nat]> σ')   to_gen_heap {[ := #(nmax-1)%nat]}))
      as (h) "[Hh● Hℓ◯]".
    {
      apply auth_valid_discrete_2 ; split.
      - rewrite - insert_delete ; set σ'' := delete  σ'.
        unfold to_gen_heap ; rewrite 2! fmap_insert fmap_empty insert_empty.
        exists (to_gen_heap σ'').
        rewrite (@gmap.insert_singleton_op _ _ _ _ (to_gen_heap σ'')) //.
        rewrite lookup_fmap ; apply fmap_None, lookup_delete.
      - apply to_gen_heap_valid.
    }
    (* allocate the ghost state associated with ℓ: *)
    iMod (auth_nat_alloc 0) as (γ1) "[Hγ1● _]".
    iMod (auth_mnat_alloc 0) as (γ2) "[Hγ2● _]".
    (* packing all those bits, build the heap instance necessary to use time credits: *)
    pose (Build_timeReceiptHeapG Σ (HeapG Σ _ (GenHeapG _ _ Σ _ _ _ h)) _ _ _ γ1 γ2)
      as HtrHeapG.
    (* create the invariant: *)
    iAssert (|={}=> TOCKCTXT nmax)%I with "[Hℓ◯ Hγ1● Hγ2●]" as "> Hinv".
    {
      iApply inv_alloc.
      iExists 0%nat. rewrite (_ : nmax - 0 - 1 = Z.of_nat (nmax - 1)) ; last lia.
      unfold mapsto ; destruct mapsto_aux as [_ ->] ; simpl.
      unfold to_gen_heap ; rewrite fmap_insert fmap_empty insert_empty.
      by iFrame.
    }
    iModIntro.
    (* finally, use the user-given specification: *)
    iExists gen_heap_ctx. iFrame "Hh●".
    iApply (Hspec with "Hinv") ; auto.
  Qed.

  Lemma spec_trtranslation__adequate {Σ} (nmax : nat) (φ : val  Prop) e :
    (0 < nmax)%nat 
    is_closed [] e 
    ( `{!timeReceiptHeapG Σ},
      TOCKCTXT nmax -
      {{{ True }}} «e» {{{ v, RET v ; ⌜φ (invtranslationV v) }}}
    ) 
     `{!timeReceiptHeapPreG Σ} σ,
      adequate_n NotStuck (nmax-1) e σ φ.
  Proof.
    intros Inmax Hclosed Hspec HpreG σ.
    eapply adequate_trtranslation__adequate ; first done.
    intros Hloc. by eapply spec_trtranslation__adequate_translation.
  Qed.
Glen Mével's avatar
Glen Mével committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

End Soundness.



(*
 * Proof-mode tactics for proving WP of translated expressions
 *)

Section Tactics.

  (* TODO *)

End Tactics.

Ltac wp_tock :=
  idtac.