invariants.v 3.61 KB
Newer Older
1 2
From iris.base_logic.lib Require Export fancy_updates namespaces.
From iris.base_logic.lib Require Import wsat.
3
From iris.algebra Require Import gmap.
4
From iris.proofmode Require Import tactics coq_tactics intro_patterns.
Ralf Jung's avatar
Ralf Jung committed
5
Import uPred.
Robbert Krebbers's avatar
Robbert Krebbers committed
6

7
(** Derived forms and lemmas about them. *)
8
Definition inv_def `{invG Σ} (N : namespace) (P : iProp Σ) : iProp Σ :=
9
  ( i, i  N  ownI i P)%I.
10
Definition inv_aux : { x | x = @inv_def }. by eexists. Qed.
11
Definition inv {Σ i} := proj1_sig inv_aux Σ i.
12
Definition inv_eq : @inv = @inv_def := proj2_sig inv_aux.
13
Instance: Params (@inv) 3.
14
Typeclasses Opaque inv.
Ralf Jung's avatar
Ralf Jung committed
15 16

Section inv.
17
Context `{invG Σ}.
Ralf Jung's avatar
Ralf Jung committed
18 19
Implicit Types i : positive.
Implicit Types N : namespace.
20
Implicit Types P Q R : iProp Σ.
Ralf Jung's avatar
Ralf Jung committed
21

22
Global Instance inv_contractive N : Contractive (inv N).
23 24 25
Proof.
  rewrite inv_eq=> n ???. apply exist_ne=>i. by apply and_ne, ownI_contractive.
Qed.
Ralf Jung's avatar
Ralf Jung committed
26

27
Global Instance inv_persistent N P : PersistentP (inv N P).
28
Proof. rewrite inv_eq /inv; apply _. Qed.
Ralf Jung's avatar
Ralf Jung committed
29

30
Lemma inv_alloc N E P :  P ={E}= inv N P.
31
Proof.
32
  rewrite inv_eq /inv_def fupd_eq /fupd_def. iIntros "HP [Hw $]".
33 34
  iMod (ownI_alloc (  N) P with "[HP Hw]") as (i) "(% & $ & ?)"; auto.
  - intros Ef. exists (coPpick ( N  coPset.of_gset Ef)).
35 36 37 38 39
    rewrite -coPset.elem_of_of_gset comm -elem_of_difference.
    apply coPpick_elem_of=> Hfin.
    eapply nclose_infinite, (difference_finite_inv _ _), Hfin.
    apply of_gset_finite.
  - by iFrame.
40
  - rewrite /uPred_except_0; eauto.
41
Qed.
42

43
Lemma inv_open E N P :
44
  N  E  inv N P ={E,E∖↑N}=  P  ( P ={E∖↑N,E}= True).
45
Proof.
46
  rewrite inv_eq /inv_def fupd_eq /fupd_def; iDestruct 1 as (i) "[Hi #HiP]".
47
  iDestruct "Hi" as % ?%elem_of_subseteq_singleton.
48 49
  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.
50 51
  iIntros "(Hw & [HE $] & $) !> !>".
  iDestruct (ownI_open i P with "[$Hw $HE $HiP]") as "($ & $ & HD)".
52
  iIntros "HP [Hw $] !> !>". iApply ownI_close; by iFrame.
53 54
Qed.

55
Lemma inv_open_timeless E N P `{!TimelessP P} :
56
  N  E  inv N P ={E,E∖↑N}= P  (P ={E∖↑N,E}= True).
57
Proof.
58 59
  iIntros (?) "Hinv". iMod (inv_open with "Hinv") as "[>HP Hclose]"; auto.
  iIntros "!> {$HP} HP". iApply "Hclose"; auto.
60
Qed.
Ralf Jung's avatar
Ralf Jung committed
61
End inv.
62 63 64 65 66

Tactic Notation "iInvCore" constr(N) "as" tactic(tac) constr(Hclose) :=
  let Htmp := iFresh in
  let patback := intro_pat.parse_one Hclose in
  let pat := constr:(IList [[IName Htmp; patback]]) in
67
  iMod (inv_open _ N with "[#]") as pat;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    [idtac|iAssumption || fail "iInv: invariant" N "not found"|idtac];
    [solve_ndisj || match goal with |- ?P => fail "iInv: cannot solve" P end
    |tac Htmp].

Tactic Notation "iInv" constr(N) "as" constr(pat) constr(Hclose) :=
   iInvCore N as (fun H => iDestruct H as pat) Hclose.
Tactic Notation "iInv" constr(N) "as" "(" simple_intropattern(x1) ")"
    constr(pat) constr(Hclose) :=
   iInvCore N as (fun H => iDestruct H as (x1) pat) Hclose.
Tactic Notation "iInv" constr(N) "as" "(" simple_intropattern(x1)
    simple_intropattern(x2) ")" constr(pat) constr(Hclose) :=
   iInvCore N as (fun H => iDestruct H as (x1 x2) pat) Hclose.
Tactic Notation "iInv" constr(N) "as" "(" simple_intropattern(x1)
    simple_intropattern(x2) simple_intropattern(x3) ")"
    constr(pat) constr(Hclose) :=
   iInvCore N as (fun H => iDestruct H as (x1 x2 x3) pat) Hclose.
Tactic Notation "iInv" constr(N) "as" "(" simple_intropattern(x1)
    simple_intropattern(x2) simple_intropattern(x3) simple_intropattern(x4) ")"
    constr(pat) constr(Hclose) :=
   iInvCore N as (fun H => iDestruct H as (x1 x2 x3 x4) pat) Hclose.