@@ -264,7 +264,7 @@ Section wp.
264264 Lemma istate_read l σ :
265265 has_prngs σ
266266 -∗ known_prng l
267- -∗ has_prngs σ ∗ ∃ n, (σ !! l) ≡ Some n.
267+ -∗ ∃ n, (σ !! l) ≡ Some n.
268268 Proof .
269269 iIntros "[HV HK] Hf".
270270 iPoseProof (own_valid_2 with "HK Hf") as "%H".
@@ -291,9 +291,9 @@ Section wp.
291291 ==∗ has_prngs (<[l:=n']> σ).
292292 Proof .
293293 iIntros "H #Hl".
294- iPoseProof (istate_read l σ with "H Hl") as "([HV HK] & [ %n %Hlookup]) ".
294+ iPoseProof (istate_read l σ with "H Hl") as "[ %n %Hlk] ".
295295 unfold has_prngs.
296-
296+ iDestruct "H" as "[HV HK]".
297297 iMod (own_update _ _ (Excl $ <[l:=n']>σ) with "HV") as "$".
298298 {
299299 by intros ?[[]|][].
@@ -366,7 +366,7 @@ Section wp.
366366 iSimpl.
367367 iApply (lc_fupd_elim_later with "Hlc").
368368 iNext.
369- iPoseProof (istate_read l σ with "Hh Hp") as "(Hh & %m & %Hread)".
369+ iPoseProof (istate_read l σ with "Hh Hp") as "(%m & %Hread)".
370370 (* current state, reification results, new state, continuation, thread pool additions *)
371371 iExists σ,(read_lcg m),(<[l:=update_lcg m]>σ),(κ (cont $ read_lcg m)),[].
372372 iFrame "Hs".
@@ -414,7 +414,7 @@ Section wp.
414414 iFrame "Hs".
415415 iSplit.
416416 {
417- iPoseProof (istate_read l σ with "Hh Hp") as "(Hh & %mm & %Hread)".
417+ iPoseProof (istate_read l σ with "Hh Hp") as "(%mm & %Hread)".
418418 rewrite /lift_post /state_seed Hread //.
419419 }
420420 iSplit; first rewrite ofe_iso_21 later_map_Next //.
@@ -444,6 +444,111 @@ Section wp.
444444 by iModIntro.
445445 Qed .
446446
447+ (* [wp_new_state], [wp_gen_state], and [wp_seed_state] are proof rules with explict and exclusive state ownership *)
448+
449+ Lemma wp_new_state (k : locO -n> IT) s σ Φ `{!NonExpansive Φ} :
450+ let l:=Loc.fresh (dom σ) in
451+ has_substate σ -∗
452+ has_prngs σ -∗
453+ ▷▷ (has_substate (<[l := 0]>σ) -∗
454+ has_prngs (<[l := 0]>σ) -∗
455+ known_prng l -∗
456+ WP@{rs} k l @ s {{ Φ }}) -∗
457+ WP@{rs} PRNG_NEW k @ s {{ Φ }}.
458+ Proof .
459+ iIntros (l) "Hs Hp Ha".
460+ iApply wp_subreify_ctx_indep_lift''.
461+ iExists σ,l,(<[l:=0]>σ),_,[].
462+ iFrame "Hs".
463+ iModIntro.
464+ iSplit; first done.
465+ iSplit; first done.
466+ iNext.
467+ iMod (istate_alloc 0 l with "Hp") as "[Hp Hl]".
468+ {
469+ apply not_elem_of_dom_1.
470+ rewrite -(Loc.add_0 l).
471+ by apply Loc.fresh_fresh.
472+ }
473+ iIntros "Hlc Hs".
474+ iModIntro.
475+ iSplit.
476+ {
477+ rewrite ofe_iso_21.
478+ iApply fupd_wp.
479+ iApply (lc_fupd_elim_later with "Hlc").
480+ iNext.
481+ iApply ("Ha" with "Hs Hp Hl").
482+ }
483+ by cbn.
484+ Qed .
485+
486+ Lemma wp_gen_state l n s σ Φ :
487+ let n' := update_lcg n in
488+ σ !! l = Some n →
489+ has_substate σ -∗
490+ has_prngs σ -∗
491+ known_prng l -∗
492+ ▷▷ (has_substate (<[l := n']>σ) -∗
493+ has_prngs (<[l := n']>σ) -∗
494+ known_prng l -∗
495+ Φ (RetV $ read_lcg n)) -∗
496+ WP@{rs} PRNG_GEN l @ s {{ Φ }}.
497+ Proof .
498+ iIntros (n' Hlk) "Hs Hp #Hl Ha".
499+ iApply wp_subreify_ctx_indep_lift''.
500+ iExists σ,(read_lcg n),(<[l:=n']>σ),_,[].
501+ iFrame "Hs".
502+ iModIntro.
503+ iSplit; first rewrite /= /state_gen Hlk //.
504+ iSplit; first done.
505+ iNext.
506+ iIntros "Hlc Hs".
507+ iMod (istate_write l (update_lcg n) σ with "Hp Hl") as "Hp".
508+ iModIntro.
509+ iSplit.
510+ {
511+ rewrite ofe_iso_21.
512+ iApply wp_val.
513+ iApply (lc_fupd_elim_later with "Hlc").
514+ iNext.
515+ iApply ("Ha" with "Hs Hp Hl").
516+ }
517+ by cbn.
518+ Qed .
519+
520+ Lemma wp_seed_state l m s σ Φ :
521+ has_substate σ -∗
522+ has_prngs σ -∗
523+ known_prng l -∗
524+ ▷▷ (has_substate (<[l := m]>σ) -∗
525+ has_prngs (<[l := m]>σ) -∗
526+ known_prng l -∗
527+ Φ (RetV ())) -∗
528+ WP@{rs} PRNG_SEED l m @ s {{ Φ }}.
529+ Proof .
530+ iIntros "Hs Hp #Hl Ha".
531+ iPoseProof (istate_read l σ with "Hp Hl") as "(%n & %Hread)".
532+ iApply wp_subreify_ctx_indep_lift''.
533+ iExists σ,(),(<[l:=m]>σ),_,[].
534+ iFrame "Hs".
535+ iModIntro.
536+ iSplit; first rewrite /= /state_seed Hread //.
537+ iSplit; first done.
538+ iNext.
539+ iIntros "Hlc Hs".
540+ iMod (istate_write l m σ with "Hp Hl") as "Hp".
541+ iModIntro.
542+ iSplit.
543+ {
544+ rewrite ofe_iso_21.
545+ iApply wp_val.
546+ iApply (lc_fupd_elim_later with "Hlc").
547+ iNext.
548+ iApply ("Ha" with "Hs Hp Hl").
549+ }
550+ by cbn.
551+ Qed .
447552End wp.
448553
449554Arguments prng_ctx {_ _} rs {_ _ _ _ _ _}.
0 commit comments