Commit c56b93ab authored by Simon Spies's avatar Simon Spies

refactor to use iris arrays more and move semantic invariants out

parent 8e68836b
Pipeline #19778 canceled with stage
......@@ -80,6 +80,7 @@ theories/logrel/F_mu_ref_conc/examples/fact.v
theories/logrel_heaplang/ltyping.v
theories/logrel_heaplang/ltyping_safety.v
theories/logrel_heaplang/lib/symbol_adt.v
theories/logrel_heaplang/lib/invariants.v
theories/logrel_heaplang/lib/arrays.v
theories/logrel_heaplang/lib/vectors.v
......
This diff is collapsed.
(* TODO: merge this into iris *)
From iris.base_logic.lib Require Export fancy_updates.
From stdpp Require Export namespaces.
From iris.base_logic.lib Require Import wsat.
From iris.algebra Require Import gmap.
From iris.proofmode Require Import tactics.
Set Default Proof Using "Type".
Import uPred.
Lemma fresh_inv_name (E : gset positive) N : i, i E i (N:coPset).
Proof.
exists (coPpick ( N gset_to_coPset E)).
rewrite -elem_of_gset_to_coPset (comm and) -elem_of_difference.
apply coPpick_elem_of=> Hfin.
eapply nclose_infinite, (difference_finite_inv _ _), Hfin.
apply gset_to_coPset_finite.
Qed.
(** * Invariants *)
Section inv.
Context `{!invG Σ}.
(** Internal backing store of invariants *)
Definition internal_inv_def (N : namespace) (P : iProp Σ) : iProp Σ :=
( i P', i (N:coPset) (P' P) ownI i P')%I.
Definition internal_inv_aux : seal (@internal_inv_def). by eexists. Qed.
Definition internal_inv := internal_inv_aux.(unseal).
Definition internal_inv_eq : @internal_inv = @internal_inv_def := internal_inv_aux.(seal_eq).
Typeclasses Opaque internal_inv.
Global Instance internal_inv_persistent N P : Persistent (internal_inv N P).
Proof. rewrite internal_inv_eq /internal_inv; apply _. Qed.
Lemma internal_inv_open E N P :
N E internal_inv N P ={E,E∖↑N}= P ( P ={E∖↑N,E}= True).
Proof.
rewrite internal_inv_eq /internal_inv_def uPred_fupd_eq /uPred_fupd_def.
iDestruct 1 as (i P') "(Hi & #HP' & #HiP)".
iDestruct "Hi" as % ?%elem_of_subseteq_singleton.
rewrite {1 4}(union_difference_L ( N) E) // ownE_op; last set_solver.
rewrite {1 5}(union_difference_L {[ i ]} ( N)) // ownE_op; last set_solver.
iIntros "(Hw & [HE $] & $) !> !>".
iDestruct (ownI_open i with "[$Hw $HE $HiP]") as "($ & HP & HD)".
iDestruct ("HP'" with "HP") as "$".
iIntros "HP [Hw $] !> !>". iApply (ownI_close _ P'). iFrame "HD Hw HiP".
iApply "HP'". iFrame.
Qed.
Lemma internal_inv_alloc N E P : P ={E}= internal_inv N P.
Proof.
rewrite internal_inv_eq /internal_inv_def uPred_fupd_eq. iIntros "HP [Hw $]".
iMod (ownI_alloc ( (N : coPset)) P with "[$HP $Hw]")
as (i ?) "[$ ?]"; auto using fresh_inv_name.
do 2 iModIntro. iExists i, P. rewrite -(iff_refl True%I). auto.
Qed.
Lemma internal_inv_alloc_open N E P :
N E (|={E, E∖↑N}=> internal_inv N P (P ={E∖↑N, E}= True))%I.
Proof.
rewrite internal_inv_eq /internal_inv_def uPred_fupd_eq. iIntros (Sub) "[Hw HE]".
iMod (ownI_alloc_open ( (N : coPset)) P with "Hw")
as (i ?) "(Hw & #Hi & HD)"; auto using fresh_inv_name.
iAssert (ownE {[i]} ownE ( N {[i]}) ownE (E N))%I
with "[HE]" as "(HEi & HEN\i & HE\N)".
{ rewrite -?ownE_op; [|set_solver..].
rewrite assoc_L -!union_difference_L //. set_solver. }
do 2 iModIntro. iFrame "HE\N". iSplitL "Hw HEi"; first by iApply "Hw".
iSplitL "Hi".
{ iExists i, P. rewrite -(iff_refl True%I). auto. }
iIntros "HP [Hw HE\N]".
iDestruct (ownI_close with "[$Hw $Hi $HP $HD]") as "[$ HEi]".
do 2 iModIntro. iSplitL; [|done].
iCombine "HEi HEN\i HE\N" as "HEN".
rewrite -?ownE_op; [|set_solver..].
rewrite assoc_L -!union_difference_L //; set_solver.
Qed.
(** Invariants API *)
Definition inv_def (N: namespace) (P: iProp Σ) : iProp Σ :=
( ( E, ⌜↑N E |={E,E N}=> P ( P ={E N,E}= True)))%I.
Definition inv_aux : seal (@inv_def). by eexists. Qed.
Definition inv := inv_aux.(unseal).
Definition inv_eq : @inv = @inv_def := inv_aux.(seal_eq).
Typeclasses Opaque inv.
(** Properties about invariants *)
Global Instance inv_contractive N: Contractive (inv N).
Proof. rewrite inv_eq. solve_contractive. Qed.
Global Instance inv_ne N : NonExpansive (inv N).
Proof. apply contractive_ne, _. Qed.
Global Instance inv_proper N: Proper (equiv ==> equiv) (inv N).
Proof. apply ne_proper, _. Qed.
Global Instance inv_persistent M P: Persistent (inv M P).
Proof. rewrite inv_eq. typeclasses eauto. Qed.
Lemma inv_acc N P Q:
inv N P - (P - Q (Q - P)) - inv N Q.
Proof.
iIntros "#I #Acc". rewrite inv_eq.
iModIntro. iIntros (E H). iDestruct ("I" $! E H) as "#I'".
iApply fupd_wand_r. iFrame "I'".
iIntros "(P & Hclose)". iSpecialize ("Acc" with "P").
iDestruct "Acc" as "[Q CB]". iFrame.
iIntros "Q". iApply "Hclose". now iApply "CB".
Qed.
Lemma inv_iff N P Q : (P Q) - inv N P - inv N Q.
Proof.
iIntros "#HPQ #I". iApply (inv_acc with "I").
iNext. iIntros "!# P". iSplitL "P".
- by iApply "HPQ".
- iIntros "Q". by iApply "HPQ".
Qed.
Lemma inv_to_inv M P: internal_inv M P - inv M P.
Proof.
iIntros "#I". rewrite inv_eq. iIntros (E H).
iPoseProof (internal_inv_open with "I") as "H"; eauto.
Qed.
Lemma inv_alloc N E P : P ={E}= inv N P.
Proof.
iIntros "P". iPoseProof (internal_inv_alloc N E with "P") as "I".
iApply fupd_mono; last eauto.
iApply inv_to_inv.
Qed.
Lemma inv_alloc_open N E P :
N E (|={E, E∖↑N}=> inv N P (P ={E∖↑N, E}= True))%I.
Proof.
iIntros (H). iPoseProof (internal_inv_alloc_open _ _ _ H) as "H".
iApply fupd_mono; last eauto.
iIntros "[I H]"; iFrame; by iApply inv_to_inv.
Qed.
Lemma inv_open E N P :
N E inv N P ={E,E∖↑N}= P ( P ={E∖↑N,E}= True).
Proof.
rewrite inv_eq /inv_def; iIntros (H) "#I". by iApply "I".
Qed.
Lemma inv_open_strong E N P :
N E inv N P ={E,E∖↑N}= P E', P ={E',N E'}= True.
Proof.
iIntros (?) "Hinv". iPoseProof (inv_open ( N) N P with "Hinv") as "H"; first done.
rewrite difference_diag_L.
iPoseProof (fupd_mask_frame_r _ _ (E N) with "H") as "H"; first set_solver.
rewrite left_id_L -union_difference_L //. iMod "H" as "[$ H]"; iModIntro.
iIntros (E') "HP".
iPoseProof (fupd_mask_frame_r _ _ E' with "(H HP)") as "H"; first set_solver.
by rewrite left_id_L.
Qed.
Global Instance into_inv_inv N P : IntoInv (inv N P) N := {}.
Global Instance into_acc_inv N P E:
IntoAcc (X := unit) (inv N P)
(N E) True (fupd E (E N)) (fupd (E N) E)
(λ _ : (), ( P)%I) (λ _ : (), ( P)%I) (λ _ : (), None).
Proof.
rewrite inv_eq /IntoAcc /accessor bi.exist_unit.
iIntros (?) "#Hinv _". iApply "Hinv"; done.
Qed.
Lemma inv_open_timeless E N P `{!Timeless P} :
N E inv N P ={E,E∖↑N}= P (P ={E∖↑N,E}= True).
Proof.
iIntros (?) "Hinv". iMod (inv_open with "Hinv") as "[>HP Hclose]"; auto.
iIntros "!> {$HP} HP". iApply "Hclose"; auto.
Qed.
(* Weakening of semantic invariants *)
Lemma inv_proj_l N P Q: inv N (P Q) - inv N P.
Proof.
iIntros "#I". iApply inv_acc; eauto.
iNext. iIntros "!# [$ Q] P"; iFrame.
Qed.
Lemma inv_proj_r N P Q: inv N (P Q) - inv N Q.
Proof.
rewrite (bi.sep_comm P Q). eapply inv_proj_l.
Qed.
Lemma inv_split N P Q: inv N (P Q) - inv N P inv N Q.
Proof.
iIntros "#H".
iPoseProof (inv_proj_l with "H") as "$".
iPoseProof (inv_proj_r with "H") as "$".
Qed.
End inv.
From iris.heap_lang Require Export lifting metatheory.
From iris.base_logic.lib Require Import invariants.
From iris.heap_lang Require Import notation proofmode.
From iris_examples.logrel_heaplang Require Import ltyping ltyping_safety.
From iris.algebra Require Import gmap auth agree.
From iris_examples.logrel_heaplang.lib Require Import arrays.
From iris_examples.logrel_heaplang.lib Require Import invariants arrays.
Set Default Proof Using "Type".
Section Vectors.
(* vec = ref array *)
......@@ -20,11 +16,9 @@ Set Default Proof Using "Type".
let: "b" := copy "a" (!"a") in
let: "v" := ref "b" in "f" #() "v".
Definition idx : val :=
λ: <> "v" "i", if: ((#-1) < "i") && ("i" < !(!"v")) then SOME "i" else NONE.
Definition get : val :=
λ: <> "v" "i", unsafe_get (!"v") "i".
......@@ -39,9 +33,15 @@ Set Default Proof Using "Type".
if: CAS "v" "a" "b" then #() else "f" "v" "x".
(* History *)
Definition Hist := gmap loc nat.
Definition HistUR := gmapUR loc (agreeR (leibnizO nat)).
Definition hist : Hist HistUR := fmap (λ v: nat, to_agree (v : leibnizO nat)).
Implicit Types (h : Hist) (a: loc).
Context `{!heapG Σ,
!inG Σ (authR mnatUR), (* maximum length *)
!inG Σ (authR (gmapUR loc (agreeR (leibnizC nat)))) (* history *)
!inG Σ (authR HistUR) (* history *)
}.
(* Maximum Length *)
......@@ -90,11 +90,7 @@ Set Default Proof Using "Type".
Qed.
(* History *)
Definition Hist := gmap loc nat.
Definition HistUR := gmapUR loc (agreeR (leibnizC nat)).
Definition hist: Hist -> HistUR := fmap (λ v, to_agree (v : leibnizC nat)).
Implicit Types (h : Hist) (a: loc).
Section hist.
(** Conversion to hists and back *)
......@@ -113,7 +109,7 @@ Set Default Proof Using "Type".
Qed.
Lemma hist_insert h a m :
hist (<[a:=m]> h) = <[a:= to_agree (m: leibnizC nat)]> (hist h).
hist (<[a:=m]> h) = <[a:= to_agree (m: leibnizO nat)]> (hist h).
Proof. by rewrite /hist fmap_insert. Qed.
Lemma hist_delete a h :
......@@ -121,10 +117,9 @@ Set Default Proof Using "Type".
Proof. by rewrite /hist fmap_delete. Qed.
End hist.
Definition hist_all γ h : iProp Σ :=
(own γ ( (hist h)))%I.
Definition hist_all γ h : iProp Σ := (own γ ( (hist h)))%I.
Definition hist_entry γ a m : iProp Σ :=
(own γ ( {[ a := to_agree (m : leibnizC nat)]}))%I.
(own γ ( {[ a := to_agree (m : leibnizO nat)]}))%I.
Global Instance hist_persistent a m γ: Persistent (hist_entry γ a m).
Proof. eapply _. Qed.
......@@ -185,11 +180,10 @@ Set Default Proof Using "Type".
Lemma Vec_project_array γ1 γ2 a m l:
hist_entry γ2 a m - Vec γ1 γ2 l - Inv N (array m a).
hist_entry γ2 a m - Vec γ1 γ2 l - inv N (array m a).
Proof.
iIntros "#E #V". iPoseProof (inv_to_Inv with "V") as "Vec".
iApply Inv_acc; eauto.
iModIntro. iDestruct 1 as (h b n) "(Hl & Hγ1● & Hγ2● & Hγ2◯ & Hst)".
iIntros "#E #V". iApply inv_acc; eauto.
iNext. iModIntro. iDestruct 1 as (h b n) "(Hl & Hγ1● & Hγ2● & Hγ2◯ & Hst)".
iAssert (h !! a = Some m)%I with "[Hγ2●]" as "#H".
{ iApply (hist_agree with "[Hγ2●]"); eauto. }
iAssert ((array m a (array m a - [ map] a m h, array (Z.of_nat m) a)))%I
......@@ -221,8 +215,8 @@ Set Default Proof Using "Type".
iIntros (v1) "!# #Array". wp_pures.
iIntros (v2) "!# #Cont". wp_pures.
iDestruct "Array" as (a m) "[Array ->]".
wp_bind (! #a)%E.
iInv "Array" as "[Ha Hl]" "Hclose". wp_load. iMod ("Hclose" with "[$Ha $Hl]") as "_".
wp_bind (! #a)%E. iPoseProof (array_length_inv with "Array") as "L".
iInv "L" as "Ha" "Hclose". wp_load. iMod ("Hclose" with "Ha") as "_".
iModIntro. wp_apply (copy_spec); eauto with lia.
iIntros (b) "B". wp_pures.
wp_bind (ref _)%E. wp_apply wp_fupd. wp_alloc l as "Hl".
......@@ -254,8 +248,9 @@ Set Default Proof Using "Type".
iMod("Hclose" with "[H1 H2 H3 H4]") as "_".
iNext; iExists h, a, m; iFrame; eauto.
iModIntro. iPoseProof (Vec_project_array with "Ha V") as "I".
wp_bind (! _)%E. iInv "I" as "[Hb Aa]" "Hclose". wp_load.
iMod("Hclose" with "[$Hb $Aa]") as "_". iModIntro. wp_pures.
wp_bind (! _)%E. iPoseProof (array_length_inv with "I") as "L".
iInv "L" as "H" "Hclose". wp_load. iMod ("Hclose" with "H") as "_".
iModIntro. wp_pures.
bool_decide H'; last (wp_pures; now iLeft).
wp_pures. iRight. iExists #n. iSplit; eauto.
destruct (Z_to_nat n) as [k ->]; first lia.
......@@ -323,39 +318,36 @@ Set Default Proof Using "Type".
iInv "Vec" as (h a m) "(Hl & Hγ1● & Hγ2 & #Ha & Hst)" "Hclose". wp_load.
iMod ("Hclose" with "[Hl Hγ1● Hγ2 Hst]") as "_". iNext. iExists h, a, m. iFrame; eauto.
clear h. iModIntro. iPoseProof (Vec_project_array with "Ha Vec") as "#HA".
wp_pures. wp_bind (! #a)%E.
iInv "HA" as "[Hal A]" "Hclose". wp_load. iMod ("Hclose" with "[$Hal $A]") as "_".
wp_pures. wp_bind (! #a)%E. iPoseProof (array_length_inv with "HA") as "L".
iInv "L" as "H" "Hclose". wp_load. iMod ("Hclose" with "H") as "_".
iModIntro. wp_pures.
wp_apply (copy_spec with "[]"); first eauto with lia.
iIntros (b) "A". wp_pures.
wp_bind (! #a)%E. iInv "HA" as "[Hb Aa]" "Hclose". wp_load.
iMod("Hclose" with "[$Hb $Aa]") as "_". iModIntro. wp_pures.
iDestruct "A" as "[Hb Hst]". rewrite /body big_opR_acc.
iDestruct "Hst" as "[Hbm Hst]".
iDestruct "Hbm" as (v') "Hbm". rewrite loc_add_assoc.
wp_store. 2: lia.
iSpecialize ("Hst" with "[Hbm]"); eauto. iCombine "Hb Hst" as "Hb".
wp_bind (CAS _ _ _).
wp_bind (! #a)%E. iInv "L" as "Hb" "Hclose". wp_load.
iMod("Hclose" with "Hb") as "_". iModIntro. wp_pures.
iPoseProof (array_entry_acc m with "[] A") as "[E A]"; first (iPureIntro; lia).
iDestruct "E" as (v) "E". wp_store. iSpecialize ("A" with "[E]"); first by iExists _.
wp_bind (CmpXchg _ _ _).
iInv "Vec" as (h a' m') "(Hl & Hγ1● & Hγ2 & #Ha' & Hst)" "Hclose".
wp_cas as H|H.
wp_cmpxchg as H|H.
- injection H as ?; subst.
iPoseProof (entry_agree with "Ha Ha'") as "%"; subst m'.
iMod (auth_mnat_update _ _ (m + 1)%nat with "Hγ1●") as "Hγ1●"; try lia.
iAssert (h !! b = None)%I with "[Hb Hst]" as "%".
iAssert (h !! b = None)%I with "[A Hst]" as "%".
{ destruct (h !! b) eqn: H; eauto.
iPoseProof (big_sepM_insert_acc with "Hst") as "Hst"; eauto.
iDestruct "Hb" as "[H1?]". iDestruct "Hst" as "[[H2?]?]".
iPoseProof (mapsto_valid_2 with "H1 H2") as "%".
exfalso.
iPoseProof (big_sepM_insert_acc with "Hst") as "[Hst _]"; eauto.
iPoseProof (array_length_acc with "A") as "[H1 _]".
iPoseProof (array_length_acc with "Hst") as "[H2 _]".
iPoseProof (mapsto_valid_2 with "H1 H2") as "%". exfalso.
destruct (frac.frac_valid' 2%Qp) as [H1 _]. eapply H1 in H0.
now eapply Qp_not_plus_q_ge_1 in H0. }
iMod (hist_update _ _ _ (m + 1)%nat with "Hγ2") as "[Hγ2 #Hb_ent]"; eauto.
iMod ("Hclose" with "[Hl Hγ1● Hγ2 Hb Hst]") as "_".
iMod ("Hclose" with "[Hl Hγ1● Hγ2 A Hst]") as "_".
iNext. iExists (<[b:=(m + 1)%nat]> h), b, ((m + 1)%nat). iFrame.
iSplit; eauto. rewrite big_sepM_insert; eauto; iFrame.
replace ((m + 1)%nat : Z) with (m + 1) by lia.
iFrame. iModIntro. wp_pures. done.
- iMod ("Hclose" with "[Hl Hγ1● Hγ2 Hb Hst]") as "_".
- iMod ("Hclose" with "[Hl Hγ1● Hγ2 A Hst]") as "_".
iNext. iExists h, a', m'. iFrame; eauto.
iModIntro. wp_pures.
iApply "IH". iModIntro. iExists γ1, γ2, l. repeat iSplit; eauto.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment