excl.v 6.75 KB
Newer Older
1
From iris.algebra Require Export cmra.
2
From iris.base_logic Require Import base_logic.
Robbert Krebbers's avatar
Robbert Krebbers committed
3 4
Local Arguments validN _ _ _ !_ /.
Local Arguments valid _ _  !_ /.
Robbert Krebbers's avatar
Robbert Krebbers committed
5 6 7 8 9 10

Inductive excl (A : Type) :=
  | Excl : A  excl A
  | ExclBot : excl A.
Arguments Excl {_} _.
Arguments ExclBot {_}.
Robbert Krebbers's avatar
Robbert Krebbers committed
11

12
Notation excl' A := (option (excl A)).
Robbert Krebbers's avatar
Robbert Krebbers committed
13 14 15
Notation Excl' x := (Some (Excl x)).
Notation ExclBot' := (Some ExclBot).

Robbert Krebbers's avatar
Robbert Krebbers committed
16 17
Instance maybe_Excl {A} : Maybe (@Excl A) := λ x,
  match x with Excl a => Some a | _ => None end.
Robbert Krebbers's avatar
Robbert Krebbers committed
18

19 20
Section excl.
Context {A : cofeT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
21 22
Implicit Types a b : A.
Implicit Types x y : excl A.
23

Robbert Krebbers's avatar
Robbert Krebbers committed
24
(* Cofe *)
25
Inductive excl_equiv : Equiv (excl A) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
26
  | Excl_equiv a b : a  b  Excl a  Excl b
Robbert Krebbers's avatar
Robbert Krebbers committed
27 28
  | ExclBot_equiv : ExclBot  ExclBot.
Existing Instance excl_equiv.
29
Inductive excl_dist : Dist (excl A) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
30
  | Excl_dist a b n : a {n} b  Excl a {n} Excl b
31
  | ExclBot_dist n : ExclBot {n} ExclBot.
Robbert Krebbers's avatar
Robbert Krebbers committed
32
Existing Instance excl_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
33

Robbert Krebbers's avatar
Robbert Krebbers committed
34
Global Instance Excl_ne n : Proper (dist n ==> dist n) (@Excl A).
35 36 37
Proof. by constructor. Qed.
Global Instance Excl_proper : Proper (() ==> ()) (@Excl A).
Proof. by constructor. Qed.
38
Global Instance Excl_inj : Inj () () (@Excl A).
39
Proof. by inversion_clear 1. Qed.
40
Global Instance Excl_dist_inj n : Inj (dist n) (dist n) (@Excl A).
41
Proof. by inversion_clear 1. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
42

43
Program Definition excl_chain (c : chain (excl A)) (a : A) : chain A :=
Robbert Krebbers's avatar
Robbert Krebbers committed
44
  {| chain_car n := match c n return _ with Excl y => y | _ => a end |}.
45
Next Obligation. intros c a n i ?; simpl. by destruct (chain_cauchy c n i). Qed.
46
Instance excl_compl : Compl (excl A) := λ c,
47
  match c 0 with Excl a => Excl (compl (excl_chain c a)) | x => x end.
48
Definition excl_cofe_mixin : CofeMixin (excl A).
Robbert Krebbers's avatar
Robbert Krebbers committed
49 50
Proof.
  split.
Robbert Krebbers's avatar
Robbert Krebbers committed
51
  - intros x y; split; [by destruct 1; constructor; apply equiv_dist|].
Robbert Krebbers's avatar
Robbert Krebbers committed
52 53
    intros Hxy; feed inversion (Hxy 1); subst; constructor; apply equiv_dist.
    by intros n; feed inversion (Hxy n).
54
  - intros n; split.
Robbert Krebbers's avatar
Robbert Krebbers committed
55
    + by intros []; constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
56
    + by destruct 1; constructor.
57
    + destruct 1; inversion_clear 1; constructor; etrans; eauto.
58
  - by inversion_clear 1; constructor; apply dist_S.
59 60 61
  - intros n c; rewrite /compl /excl_compl.
    feed inversion (chain_cauchy c 0 n); first auto with lia; constructor.
    rewrite (conv_compl n (excl_chain c _)) /=. destruct (c n); naive_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
62
Qed.
63
Canonical Structure exclC : cofeT := CofeT (excl A) excl_cofe_mixin.
64 65 66 67
Global Instance excl_discrete : Discrete A  Discrete exclC.
Proof. by inversion_clear 2; constructor; apply (timeless _). Qed.
Global Instance excl_leibniz : LeibnizEquiv A  LeibnizEquiv (excl A).
Proof. by destruct 2; f_equal; apply leibniz_equiv. Qed.
68

Robbert Krebbers's avatar
Robbert Krebbers committed
69
Global Instance Excl_timeless a : Timeless a  Timeless (Excl a).
Robbert Krebbers's avatar
Robbert Krebbers committed
70
Proof. by inversion_clear 2; constructor; apply (timeless _). Qed.
71
Global Instance ExclBot_timeless : Timeless (@ExclBot A).
Robbert Krebbers's avatar
Robbert Krebbers committed
72
Proof. by inversion_clear 1; constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
73 74

(* CMRA *)
75
Instance excl_valid : Valid (excl A) := λ x,
Robbert Krebbers's avatar
Robbert Krebbers committed
76
  match x with Excl _ => True | ExclBot => False end.
77
Instance excl_validN : ValidN (excl A) := λ n x,
Robbert Krebbers's avatar
Robbert Krebbers committed
78 79 80
  match x with Excl _ => True | ExclBot => False end.
Instance excl_pcore : PCore (excl A) := λ _, None.
Instance excl_op : Op (excl A) := λ x y, ExclBot.
Robbert Krebbers's avatar
Robbert Krebbers committed
81

82
Lemma excl_cmra_mixin : CMRAMixin (excl A).
Robbert Krebbers's avatar
Robbert Krebbers committed
83
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
84
  split; try discriminate.
85 86
  - by intros n []; destruct 1; constructor.
  - by destruct 1; intros ?.
87
  - intros x; split. done. by move=> /(_ 0).
Robbert Krebbers's avatar
Robbert Krebbers committed
88 89 90 91
  - intros n [?|]; simpl; auto with lia.
  - by intros [?|] [?|] [?|]; constructor.
  - by intros [?|] [?|]; constructor.
  - by intros n [?|] [?|].
92
  - intros n x [?|] [?|] ?; inversion_clear 1; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
93
Qed.
94
Canonical Structure exclR :=
95
  CMRAT (excl A) excl_cofe_mixin excl_cmra_mixin.
96

97
Global Instance excl_cmra_discrete : Discrete A  CMRADiscrete exclR.
98 99
Proof. split. apply _. by intros []. Qed.

100 101
(** Internalized properties *)
Lemma excl_equivI {M} (x y : excl A) :
102 103 104 105 106
  x  y  (match x, y with
            | Excl a, Excl b => a  b
            | ExclBot, ExclBot => True
            | _, _ => False
            end : uPred M).
107 108 109
Proof.
  uPred.unseal. do 2 split. by destruct 1. by destruct x, y; try constructor.
Qed.
110
Lemma excl_validI {M} (x : excl A) :
Robbert Krebbers's avatar
Robbert Krebbers committed
111
   x  (if x is ExclBot then False else True : uPred M).
112
Proof. uPred.unseal. by destruct x. Qed.
113

114 115 116
(** Exclusive *)
Global Instance excl_exclusive x : Exclusive x.
Proof. by destruct x; intros n []. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
117 118 119 120 121 122

(** Option excl *)
Lemma excl_validN_inv_l n mx a : {n} (Excl' a  mx)  mx = None.
Proof. by destruct mx. Qed.
Lemma excl_validN_inv_r n mx a : {n} (mx  Excl' a)  mx = None.
Proof. by destruct mx. Qed.
123 124 125 126 127

Lemma Excl_includedN n a b  : Excl' a {n} Excl' b  a {n} b.
Proof. by intros [[c|] Hb%(inj Some)]; inversion_clear Hb. Qed.
Lemma Excl_included a b : Excl' a  Excl' b  a  b.
Proof. by intros [[c|] Hb%(inj Some)]; inversion_clear Hb. Qed.
128 129 130
End excl.

Arguments exclC : clear implicits.
131
Arguments exclR : clear implicits.
Robbert Krebbers's avatar
Robbert Krebbers committed
132

Robbert Krebbers's avatar
Robbert Krebbers committed
133
(* Functor *)
134
Definition excl_map {A B} (f : A  B) (x : excl A) : excl B :=
Robbert Krebbers's avatar
Robbert Krebbers committed
135
  match x with Excl a => Excl (f a) | ExclBot => ExclBot end.
136
Lemma excl_map_id {A} (x : excl A) : excl_map id x = x.
137
Proof. by destruct x. Qed.
138 139
Lemma excl_map_compose {A B C} (f : A  B) (g : B  C) (x : excl A) :
  excl_map (g  f) x = excl_map g (excl_map f x).
140
Proof. by destruct x. Qed.
141 142 143
Lemma excl_map_ext {A B : cofeT} (f g : A  B) x :
  ( x, f x  g x)  excl_map f x  excl_map g x.
Proof. by destruct x; constructor. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
144
Instance excl_map_ne {A B : cofeT} n :
145
  Proper ((dist n ==> dist n) ==> dist n ==> dist n) (@excl_map A B).
Robbert Krebbers's avatar
Robbert Krebbers committed
146
Proof. by intros f f' Hf; destruct 1; constructor; apply Hf. Qed.
147 148
Instance excl_map_cmra_monotone {A B : cofeT} (f : A  B) :
  ( n, Proper (dist n ==> dist n) f)  CMRAMonotone (excl_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
149
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
150
  split; try apply _.
Robbert Krebbers's avatar
Robbert Krebbers committed
151
  - by intros n [a|].
Robbert Krebbers's avatar
Robbert Krebbers committed
152 153
  - intros x y [z Hy]; exists (excl_map f z); apply equiv_dist=> n.
    move: Hy=> /equiv_dist /(_ n) ->; by destruct x, z.
Robbert Krebbers's avatar
Robbert Krebbers committed
154
Qed.
155
Definition exclC_map {A B} (f : A -n> B) : exclC A -n> exclC B :=
156
  CofeMor (excl_map f).
157
Instance exclC_map_ne A B n : Proper (dist n ==> dist n) (@exclC_map A B).
Robbert Krebbers's avatar
Robbert Krebbers committed
158
Proof. by intros f f' Hf []; constructor; apply Hf. Qed.
Ralf Jung's avatar
Ralf Jung committed
159

Robbert Krebbers's avatar
Robbert Krebbers committed
160 161 162
Program Definition exclRF (F : cFunctor) : rFunctor := {|
  rFunctor_car A B := (exclR (cFunctor_car F A B));
  rFunctor_map A1 A2 B1 B2 fg := exclC_map (cFunctor_map F fg)
163
|}.
164 165 166
Next Obligation.
  intros F A1 A2 B1 B2 n x1 x2 ??. by apply exclC_map_ne, cFunctor_ne.
Qed.
167 168 169 170 171 172 173 174
Next Obligation.
  intros F A B x; simpl. rewrite -{2}(excl_map_id x).
  apply excl_map_ext=>y. by rewrite cFunctor_id.
Qed.
Next Obligation.
  intros F A1 A2 A3 B1 B2 B3 f g f' g' x; simpl. rewrite -excl_map_compose.
  apply excl_map_ext=>y; apply cFunctor_compose.
Qed.
175

Robbert Krebbers's avatar
Robbert Krebbers committed
176 177
Instance exclRF_contractive F :
  cFunctorContractive F  rFunctorContractive (exclRF F).
178
Proof.
179
  intros A1 A2 B1 B2 n x1 x2 ??. by apply exclC_map_ne, cFunctor_contractive.
180
Qed.