Skip to content

Commit cf94b0f

Browse files
committed
PRNG gitree combinators with continunation
1 parent 1e29f6d commit cf94b0f

1 file changed

Lines changed: 111 additions & 96 deletions

File tree

theories/effects/prng.v

Lines changed: 111 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -128,35 +128,51 @@ Section prng_combinators.
128128
Notation opid_gen := (inr opid_del).
129129
Notation opid_seed := (inr opid_gen).
130130

131+
(* XXX: we have to specify [op] otherwise a weird proof obligation will be generated. *)
131132
Program Definition PRNG_NEW : (locO -n> IT) -n> IT :=
132133
λne k, Vis (E:=E) (subEff_opid opid_new)
133134
(subEff_ins (F:=prngE) (op:=opid_new) ())
134135
(NextO ◎ k ◎ (subEff_outs (op:=opid_new) ^-1)).
135-
(* XXX: we have to specify [op] otherwise a weird proof obligation will be generated. *)
136136
Solve Obligations with solve_proper.
137137

138-
Program Definition PRNG_DEL : locO -n> IT :=
139-
λne l, Vis (E:=E) (subEff_opid opid_del)
140-
(subEff_ins (F:=prngE) (op:=opid_del) l)
141-
(λne _, Next (Ret ())).
138+
Program Definition PRNG_DEL_k : locO -n> (unitO -n> IT) -n> IT :=
139+
λne l k, Vis (E:=E) (subEff_opid opid_del)
140+
(subEff_ins (F:=prngE) (op:=opid_del) l)
141+
(NextO ◎ k ◎ (subEff_outs (op:=opid_del) ^-1)).
142+
Solve Obligations with solve_proper.
143+
Definition PRNG_DEL := λne l, PRNG_DEL_k l Ret.
142144

143-
Program Definition PRNG_GEN : locO -n> IT :=
144-
λne l, Vis (E:=E) (subEff_opid $ opid_gen)
145-
(subEff_ins (F:=prngE) (op:=opid_gen) l)
146-
(NextO ◎ Ret ◎ (subEff_outs ^-1)).
145+
Program Definition PRNG_GEN_k : locO -n> (natO -n> IT) -n> IT :=
146+
λne l k, Vis (E:=E) (subEff_opid $ opid_gen)
147+
(subEff_ins (F:=prngE) (op:=opid_gen) l)
148+
(NextO ◎ k ◎ (subEff_outs (op:=opid_gen)^-1)).
149+
Solve Obligations with solve_proper.
150+
Definition PRNG_GEN := λne l, PRNG_GEN_k l Ret.
147151

148-
Program Definition PRNG_SEED : locO -n> natO -n> IT :=
149-
λne l n, Vis (E:=E) (subEff_opid $ opid_seed)
150-
(subEff_ins (F:=prngE) (op:=opid_seed) (l, n))
151-
(λne _, Next (Ret ())).
152+
Program Definition PRNG_SEED_k : locO -n> natO -n> (unitO -n> IT) -n> IT :=
153+
λne l n k, Vis (E:=E) (subEff_opid $ opid_seed)
154+
(subEff_ins (F:=prngE) (op:=opid_seed) (l, n))
155+
(NextO ◎ k ◎ (subEff_outs (op:=opid_seed) ^-1)).
156+
Solve Obligations with solve_proper.
157+
Definition PRNG_SEED := λne l n, PRNG_SEED_k l n Ret.
152158

153-
Lemma hom_NEW k f `{!IT_hom f} : f (PRNG_NEW k) ≡ PRNG_NEW (OfeMor f ◎ k).
154-
Proof.
155-
unfold PRNG_NEW.
156-
rewrite hom_vis/=. repeat f_equiv.
157-
intro x. cbn-[laterO_map]. rewrite laterO_map_Next.
159+
Ltac solve_hom_easy symbol :=
160+
unfold symbol;
161+
rewrite hom_vis/=; repeat f_equiv;
162+
intro x; cbn-[laterO_map]; rewrite laterO_map_Next;
158163
done.
159-
Qed.
164+
165+
Lemma hom_NEW k f `{!IT_hom f} : f (PRNG_NEW k) ≡ PRNG_NEW (OfeMor f ◎ k).
166+
Proof. solve_hom_easy PRNG_NEW. Qed.
167+
168+
Lemma hom_DEL_k l k f `{!IT_hom f} : f (PRNG_DEL_k l k) ≡ PRNG_DEL_k l (OfeMor f ◎ k).
169+
Proof. solve_hom_easy PRNG_DEL_k. Qed.
170+
171+
Lemma hom_GEN_k l k f `{!IT_hom f} : f (PRNG_GEN_k l k) ≡ PRNG_GEN_k l (OfeMor f ◎ k).
172+
Proof. solve_hom_easy PRNG_GEN_k. Qed.
173+
174+
Lemma hom_SEED_k l n k f `{!IT_hom f} : f (PRNG_SEED_k l n k) ≡ PRNG_SEED_k l n (OfeMor f ◎ k).
175+
Proof. solve_hom_easy PRNG_SEED_k. Qed.
160176
End prng_combinators.
161177

162178
Section wp.
@@ -170,7 +186,6 @@ Section wp.
170186
Notation ITV := (ITV F R).
171187
Notation stateO := (stateF ♯ IT).
172188

173-
(* TODO: what exactly is this CMRA? Can we get rid of the authoratative RA? *)
174189
Definition istate := gmap_viewR loc natO.
175190
Class prngPreG Σ := PrngPreG { PrngPreG_inG :: inG Σ istate }.
176191
Class prngG Σ := PrngG {
@@ -257,33 +272,34 @@ Section wp.
257272
done.
258273
Qed.
259274

260-
(* TODO: Piecing together proofs in [store.v]. Review the proof. Understand the mask changes. *)
261-
Lemma wp_gen_hom (l : loc) (n : nat) s Φ (κ : IT -n> IT) `{!IT_hom κ} :
275+
276+
Lemma wp_new_hom (k : locO -n> IT) s Φ `{!NonExpansive Φ} (κ : IT -n> IT) `{!IT_hom κ} :
262277
prng_ctx -∗
263-
▷ has_prng_state l n -∗
264-
▷▷ (has_prng_state l (update_lcg n) -∗ WP@{rs} κ (Ret $ read_lcg n) @ s {{ Φ }}) -∗
265-
WP@{rs} κ (PRNG_GEN l) @ s {{ Φ }}.
278+
▷▷ (∀ l, has_prng_state l 0 -∗ WP@{rs} κ (k l) @ s {{ Φ }}) -∗
279+
WP@{rs} κ (PRNG_NEW k) @ s {{ Φ }}.
266280
Proof.
267-
iIntros "#Hctx Hp Ha".
268-
unfold PRNG_GEN; simpl.
281+
iIntros "#Hctx Ha".
282+
unfold PRNG_NEW; simpl.
269283
rewrite hom_vis.
270284
iApply wp_subreify_ctx_indep_lift''.
271285
iInv (nroot.@"prngE") as (σ) "[>Hlc [Hs Hh]]" "Hcl".
272286
simpl.
273287
iApply (lc_fupd_elim_later with "Hlc").
274288
iNext.
289+
set (l:=Loc.fresh (dom σ)).
275290
(* current state, reification results, new state, continuation, thread pool additions *)
276-
iExists σ,(read_lcg n),(<[l:=update_lcg n]>σ),(κ (Ret $ read_lcg n)),[].
291+
iExists σ,l,(<[l:=0]>σ),(κ $ k l),[].
277292
iFrame "Hs".
278-
iSplit.
293+
iSplit; first done.
294+
iSplit; first by rewrite later_map_Next ofe_iso_21.
295+
iNext.
296+
iMod (istate_alloc 0 l with "Hh") as "[Hh Hp]".
279297
{
280-
iPoseProof (istate_read l n σ with "Hh Hp") as "%Hread".
281-
unfold lift_post, state_gen.
282-
by rewrite Hread.
298+
apply (not_elem_of_dom_1 (M:=gmap loc)).
299+
rewrite -(Loc.add_0 l).
300+
apply Loc.fresh_fresh.
301+
lia.
283302
}
284-
iSplit; first by rewrite ofe_iso_21 later_map_Next.
285-
iNext.
286-
iMod (istate_write l n (update_lcg n) σ with "Hh Hp") as "[Hh Hp]".
287303
iIntros "Hlc Hs".
288304
iMod ("Hcl" with "[Hlc Hh Hs]") as "Hemp".
289305
{ iExists _. iFrame. }
@@ -293,42 +309,44 @@ Section wp.
293309
- by iFrame.
294310
Qed.
295311

296-
Lemma wp_gen (l : loc) (n : nat) s Φ :
312+
Lemma wp_new (k : locO -n> IT) s Φ `{!NonExpansive Φ} :
297313
prng_ctx -∗
298-
▷ has_prng_state l n -∗
299-
▷ ▷ (has_prng_state l (update_lcg n) -∗ WP@{rs} (Ret $ read_lcg n) @ s {{ Φ }}) -∗
300-
WP@{rs} PRNG_GEN l @ s {{ Φ }}.
314+
▷▷ (∀ l, has_prng_state l 0 -∗ WP@{rs} k l @ s {{ Φ }}) -∗
315+
WP@{rs} PRNG_NEW k @ s {{ Φ }}.
301316
Proof.
302-
iIntros "#Hcxt Hp Ha".
303-
iApply (wp_gen_hom _ _ _ _ idfun with "Hcxt Hp Ha").
317+
iIntros "Hh H".
318+
iApply (wp_new_hom _ _ _ idfun with "Hh H").
304319
Qed.
305320

306-
Lemma wp_seed_hom (l : loc) (n n' : nat) s Φ (κ : IT -n> IT) `{!IT_hom κ} :
321+
Lemma wp_del_k_hom (l : loc) (cont : unitO -n> IT) n s Φ (κ : IT -n> IT) `{!IT_hom κ} :
307322
prng_ctx -∗
308323
▷ has_prng_state l n -∗
309-
▷▷ (has_prng_state l n' -∗ WP@{rs} κ (Ret ()) @ s {{ Φ }}) -∗
310-
WP@{rs} κ (PRNG_SEED l n') @ s {{ Φ }}.
324+
▷▷ WP@{rs} κ (cont ()) @ s {{ β, Φ β }} -∗
325+
WP@{rs} κ (PRNG_DEL_k l cont) @ s {{ Φ }}.
311326
Proof.
312327
iIntros "#Hctx Hp Ha".
313-
unfold PRNG_SEED; simpl.
328+
unfold PRNG_DEL_k; simpl.
314329
rewrite hom_vis.
315330
iApply wp_subreify_ctx_indep_lift''.
316331
iInv (nroot.@"prngE") as (σ) "[>Hlc [Hs Hh]]" "Hcl".
317332
simpl.
318333
iApply (lc_fupd_elim_later with "Hlc").
319334
iNext.
335+
iAssert (⌜is_Some (σ !! l)⌝)%I as "%Hdom".
336+
{ iApply (istate_loc_dom with "Hh Hp"). }
337+
destruct Hdom as [x Hx].
320338
(* current state, reification results, new state, continuation, thread pool additions *)
321-
iExists σ,(),(<[l:=n']>σ),(κ (Ret ())),[].
339+
iExists σ,(),(delete l σ),(κ $ cont ()),[].
322340
iFrame "Hs".
323341
iSplit.
324342
{
325343
iPoseProof (istate_read l n σ with "Hh Hp") as "%Hread".
326-
unfold lift_post, state_seed.
344+
unfold lift_post, state_del.
327345
by rewrite Hread.
328346
}
329-
iSplit; first by rewrite later_map_Next.
347+
iSplit; first by rewrite later_map_Next ofe_iso_21.
330348
iNext.
331-
iMod (istate_write l n n' σ with "Hh Hp") as "[Hh Hp]".
349+
iMod (istate_delete l n σ with "Hh Hp") as "Hh".
332350
iIntros "Hlc Hs".
333351
iMod ("Hcl" with "[Hlc Hh Hs]") as "Hemp".
334352
{ iExists _. iFrame. }
@@ -338,49 +356,45 @@ Section wp.
338356
- by iFrame.
339357
Qed.
340358

341-
Lemma wp_seed (l : loc) (n n' : nat) s Φ :
359+
Lemma wp_del (l : loc) n s Φ :
342360
prng_ctx -∗
343361
▷ has_prng_state l n -∗
344-
▷ (has_prng_state l n' -∗ Φ (RetV ())) -∗
345-
WP@{rs} PRNG_SEED l n' @ s {{ Φ }}.
362+
Φ (RetV ()) -∗
363+
WP@{rs} PRNG_DEL l @ s {{ Φ }}.
346364
Proof.
347-
iIntros "#Hctx Hp Ha".
348-
iApply (wp_seed_hom _ _ _ _ _ idfun with "Hctx Hp [Ha]").
365+
iIntros "#Hctx Hl H".
366+
iApply (wp_del_k_hom _ _ _ _ _ idfun with "Hctx Hl [H]").
349367
do 2 iNext.
350-
iIntros "H".
351-
iDestruct ("Ha" with "H") as "G".
352368
iApply wp_val.
353-
by iModIntro.
369+
iModIntro. done.
354370
Qed.
355371

356-
Lemma wp_new_hom (k : locO -n> IT) s Φ `{!NonExpansive Φ}
357-
(κ : IT -n> IT) `{!IT_hom κ} :
372+
Lemma wp_gen_k_hom (l : loc) (cont : natO -n> IT) n s Φ (κ : IT -n> IT) `{!IT_hom κ} :
358373
prng_ctx -∗
359-
▷▷ (∀ l, has_prng_state l 0 -∗ WP@{rs} κ (k l) @ s {{ Φ }}) -∗
360-
WP@{rs} κ (PRNG_NEW k) @ s {{ Φ }}.
374+
▷ has_prng_state l n -∗
375+
▷▷ (has_prng_state l (update_lcg n) -∗ WP@{rs} κ (cont $ read_lcg n) @ s {{ Φ }}) -∗
376+
WP@{rs} κ (PRNG_GEN_k l cont) @ s {{ Φ }}.
361377
Proof.
362-
iIntros "#Hctx Ha".
363-
unfold PRNG_NEW; simpl.
378+
iIntros "#Hctx Hp Ha".
379+
unfold PRNG_GEN_k; simpl.
364380
rewrite hom_vis.
365381
iApply wp_subreify_ctx_indep_lift''.
366382
iInv (nroot.@"prngE") as (σ) "[>Hlc [Hs Hh]]" "Hcl".
367383
simpl.
368384
iApply (lc_fupd_elim_later with "Hlc").
369385
iNext.
370-
set (l:=Loc.fresh (dom σ)).
371386
(* current state, reification results, new state, continuation, thread pool additions *)
372-
iExists σ,l,(<[l:=0]>σ),(κ $ k l),[].
387+
iExists σ,(read_lcg n),(<[l:=update_lcg n]>σ),(κ (cont $ read_lcg n)),[].
373388
iFrame "Hs".
374-
iSplit; first done.
375-
iSplit; first by rewrite later_map_Next ofe_iso_21.
376-
iNext.
377-
iMod (istate_alloc 0 l with "Hh") as "[Hh Hp]".
389+
iSplit.
378390
{
379-
apply (not_elem_of_dom_1 (M:=gmap loc)).
380-
rewrite -(Loc.add_0 l).
381-
apply Loc.fresh_fresh.
382-
lia.
391+
iPoseProof (istate_read l n σ with "Hh Hp") as "%Hread".
392+
unfold lift_post, state_gen.
393+
by rewrite Hread.
383394
}
395+
iSplit; first by rewrite ofe_iso_21 later_map_Next.
396+
iNext.
397+
iMod (istate_write l n (update_lcg n) σ with "Hh Hp") as "[Hh Hp]".
384398
iIntros "Hlc Hs".
385399
iMod ("Hcl" with "[Hlc Hh Hs]") as "Hemp".
386400
{ iExists _. iFrame. }
@@ -390,44 +404,42 @@ Section wp.
390404
- by iFrame.
391405
Qed.
392406

393-
Lemma wp_new (k : locO -n> IT) s Φ `{!NonExpansive Φ} :
407+
Lemma wp_gen (l : loc) (n : nat) s Φ :
394408
prng_ctx -∗
395-
▷▷ (∀ l, has_prng_state l 0 -∗ WP@{rs} k l @ s {{ Φ }}) -∗
396-
WP@{rs} PRNG_NEW k @ s {{ Φ }}.
409+
▷ has_prng_state l n -∗
410+
▷ ▷ (has_prng_state l (update_lcg n) -∗ WP@{rs} (Ret $ read_lcg n) @ s {{ Φ }}) -∗
411+
WP@{rs} PRNG_GEN l @ s {{ Φ }}.
397412
Proof.
398-
iIntros "Hh H".
399-
iApply (wp_new_hom _ _ _ idfun with "Hh H").
413+
iIntros "#Hcxt Hp Ha".
414+
iApply (wp_gen_k_hom l Ret _ _ _ idfun with "Hcxt Hp Ha").
400415
Qed.
401416

402-
Lemma wp_del_hom (l : loc) n s Φ (κ : IT -n> IT) `{!IT_hom κ} :
417+
Lemma wp_seed_k_hom (l : loc) n (cont : unitO -n> IT) n' s Φ (κ : IT -n> IT) `{!IT_hom κ} :
403418
prng_ctx -∗
404419
▷ has_prng_state l n -∗
405-
WP@{rs} κ (Ret ()) @ s {{ β, Φ β }} -∗
406-
WP@{rs} κ (PRNG_DEL l) @ s {{ Φ }}.
420+
▷ (has_prng_state l n' -∗ WP@{rs} κ (cont ()) @ s {{ Φ }}) -∗
421+
WP@{rs} κ (PRNG_SEED_k l n' cont) @ s {{ Φ }}.
407422
Proof.
408423
iIntros "#Hctx Hp Ha".
409-
unfold PRNG_DEL; simpl.
424+
unfold PRNG_SEED_k; simpl.
410425
rewrite hom_vis.
411426
iApply wp_subreify_ctx_indep_lift''.
412427
iInv (nroot.@"prngE") as (σ) "[>Hlc [Hs Hh]]" "Hcl".
413428
simpl.
414429
iApply (lc_fupd_elim_later with "Hlc").
415430
iNext.
416-
iAssert (⌜is_Some (σ !! l)⌝)%I as "%Hdom".
417-
{ iApply (istate_loc_dom with "Hh Hp"). }
418-
destruct Hdom as [x Hx].
419431
(* current state, reification results, new state, continuation, thread pool additions *)
420-
iExists σ,(),(delete l σ),(κ $ Ret ()),[].
432+
iExists σ,(),(<[l:=n']>σ),(κ (cont ())),[].
421433
iFrame "Hs".
422434
iSplit.
423435
{
424436
iPoseProof (istate_read l n σ with "Hh Hp") as "%Hread".
425-
unfold lift_post, state_del.
437+
unfold lift_post, state_seed.
426438
by rewrite Hread.
427439
}
428-
iSplit; first by rewrite later_map_Next.
440+
iSplit; first by rewrite ofe_iso_21 later_map_Next.
429441
iNext.
430-
iMod (istate_delete l n σ with "Hh Hp") as "Hh".
442+
iMod (istate_write l n n' σ with "Hh Hp") as "[Hh Hp]".
431443
iIntros "Hlc Hs".
432444
iMod ("Hcl" with "[Hlc Hh Hs]") as "Hemp".
433445
{ iExists _. iFrame. }
@@ -437,21 +449,24 @@ Section wp.
437449
- by iFrame.
438450
Qed.
439451

440-
Lemma wp_del (l : loc) n s Φ :
452+
Lemma wp_seed (l : loc) n n' s Φ :
441453
prng_ctx -∗
442454
▷ has_prng_state l n -∗
443-
Φ (RetV ()) -∗
444-
WP@{rs} PRNG_DEL l @ s {{ Φ }}.
455+
▷ (has_prng_state l n' -∗ Φ (RetV ())) -∗
456+
WP@{rs} PRNG_SEED l n' @ s {{ Φ }}.
445457
Proof.
446-
iIntros "#Hctx Hl H".
447-
iApply (wp_del_hom _ _ _ _ idfun with "Hctx Hl [H]").
458+
iIntros "#Hctx Hp Ha".
459+
iApply (wp_seed_k_hom l n Ret _ _ _ idfun with "Hctx Hp [Ha]").
448460
do 2 iNext.
461+
iIntros "H".
462+
iDestruct ("Ha" with "H") as "G".
449463
iApply wp_val.
450-
iModIntro. done.
464+
by iModIntro.
451465
Qed.
466+
452467
End wp.
453468

454469
Arguments prng_ctx {_ _} rs {_ _ _ _ _ _}.
455470
Arguments has_prng_state {_ _} _ _.
456-
#[global] Opaque PRNG_NEW PRNG_DEL PRNG_GEN PRNG_SEED.
471+
#[global] Opaque PRNG_NEW PRNG_DEL_k PRNG_GEN_k PRNG_SEED_k.
457472

0 commit comments

Comments
 (0)