option.v 8.52 KB
Newer Older
1
From algebra Require Export cmra.
2
From algebra Require Import functor upred.
3
4

(* COFE *)
5
6
7
Section cofe.
Context {A : cofeT}.
Inductive option_dist : Dist (option A) :=
8
9
  | Some_dist n x y : x {n} y  Some x {n} Some y
  | None_dist n : None {n} None.
10
Existing Instance option_dist.
11
Program Definition option_chain
12
13
14
    (c : chain (option A)) (x : A) (H : c 1 = Some x) : chain A :=
  {| chain_car n := from_option x (c n) |}.
Next Obligation.
15
  intros c x ? n [|i] ?; [omega|]; simpl.
16
  destruct (c 1) eqn:?; simplify_eq/=.
17
  by feed inversion (chain_cauchy c n (S i)).
18
Qed.
19
Instance option_compl : Compl (option A) := λ c,
20
21
22
  match Some_dec (c 1) with
  | inleft (exist x H) => Some (compl (option_chain c x H)) | inright _ => None
  end.
23
Definition option_cofe_mixin : CofeMixin (option A).
24
25
Proof.
  split.
26
  - intros mx my; split; [by destruct 1; constructor; apply equiv_dist|].
27
28
    intros Hxy; feed inversion (Hxy 1); subst; constructor; apply equiv_dist.
    by intros n; feed inversion (Hxy n).
29
  - intros n; split.
30
31
    + by intros [x|]; constructor.
    + by destruct 1; constructor.
32
    + destruct 1; inversion_clear 1; constructor; etrans; eauto.
33
  - by inversion_clear 1; constructor; apply dist_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
34
  - intros n c; unfold compl, option_compl.
35
    destruct (Some_dec (c 1)) as [[x Hx]|].
36
37
    { assert (is_Some (c (S n))) as [y Hy].
      { feed inversion (chain_cauchy c 0 (S n)); eauto with lia congruence. }
38
      rewrite Hy; constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
39
      by rewrite (conv_compl n (option_chain c x Hx)) /= Hy. }
40
41
    feed inversion (chain_cauchy c 0 (S n)); eauto with lia congruence.
    constructor.
42
Qed.
43
Canonical Structure optionC := CofeT option_cofe_mixin.
44
45
46
Global Instance option_discrete : Discrete A  Discrete optionC.
Proof. inversion_clear 2; constructor; by apply (timeless _). Qed.

47
Global Instance Some_ne : Proper (dist n ==> dist n) (@Some A).
48
Proof. by constructor. Qed.
49
Global Instance is_Some_ne n : Proper (dist n ==> iff) (@is_Some A).
Robbert Krebbers's avatar
Robbert Krebbers committed
50
Proof. inversion_clear 1; split; eauto. Qed.
51
Global Instance Some_dist_inj : Inj (dist n) (dist n) (@Some A).
Robbert Krebbers's avatar
Robbert Krebbers committed
52
Proof. by inversion_clear 1. Qed.
53
Global Instance None_timeless : Timeless (@None A).
54
Proof. inversion_clear 1; constructor. Qed.
55
Global Instance Some_timeless x : Timeless x  Timeless (Some x).
56
Proof. by intros ?; inversion_clear 1; constructor; apply timeless. Qed.
57
58
59
60
End cofe.

Arguments optionC : clear implicits.

61
(* CMRA *)
62
63
64
Section cmra.
Context {A : cmraT}.

65
66
Instance option_valid : Valid (option A) := λ mx,
  match mx with Some x =>  x | None => True end.
67
Instance option_validN : ValidN (option A) := λ n mx,
68
  match mx with Some x => {n} x | None => True end.
69
Global Instance option_empty : Empty (option A) := None.
70
71
72
Instance option_unit : Unit (option A) := fmap unit.
Instance option_op : Op (option A) := union_with (λ x y, Some (x  y)).
Instance option_minus : Minus (option A) :=
73
  difference_with (λ x y, Some (x  y)).
Robbert Krebbers's avatar
Robbert Krebbers committed
74

Robbert Krebbers's avatar
Robbert Krebbers committed
75
76
Definition Some_op a b : Some (a  b) = Some a  Some b := eq_refl.

Robbert Krebbers's avatar
Robbert Krebbers committed
77
78
79
80
81
82
83
84
85
86
87
88
Lemma option_included (mx my : option A) :
  mx  my  mx = None   x y, mx = Some x  my = Some y  x  y.
Proof.
  split.
  - intros [mz Hmz].
    destruct mx as [x|]; [right|by left].
    destruct my as [y|]; [exists x, y|destruct mz; inversion_clear Hmz].
    destruct mz as [z|]; inversion_clear Hmz; split_and?; auto;
      setoid_subst; eauto using cmra_included_l.
  - intros [->|(x&y&->&->&z&Hz)]; try (by exists my; destruct my; constructor).
    by exists (Some z); constructor.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
89

90
Definition option_cmra_mixin  : CMRAMixin (option A).
91
92
Proof.
  split.
93
94
95
96
  - by intros n [x|]; destruct 1; constructor; cofe_subst.
  - by destruct 1; constructor; cofe_subst.
  - by destruct 1; rewrite /validN /option_validN //=; cofe_subst.
  - by destruct 1; inversion_clear 1; constructor; cofe_subst.
97
  - intros [x|]; [apply cmra_valid_validN|done].
98
99
100
101
102
  - intros n [x|]; unfold validN, option_validN; eauto using cmra_validN_S.
  - intros [x|] [y|] [z|]; constructor; rewrite ?assoc; auto.
  - intros [x|] [y|]; constructor; rewrite 1?comm; auto.
  - by intros [x|]; constructor; rewrite cmra_unit_l.
  - by intros [x|]; constructor; rewrite cmra_unit_idemp.
Robbert Krebbers's avatar
Robbert Krebbers committed
103
104
  - intros mx my; rewrite !option_included ;intros [->|(x&y&->&->&?)]; auto.
    right; exists (unit x), (unit y); eauto using cmra_unit_preserving.
105
  - intros n [x|] [y|]; rewrite /validN /option_validN /=;
106
      eauto using cmra_validN_op_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
107
  - intros mx my; rewrite option_included.
108
    intros [->|(x&y&->&->&?)]; [by destruct my|].
109
    by constructor; apply cmra_op_minus.
110
111
112
113
114
115
116
117
118
  - intros n mx my1 my2.
    destruct mx as [x|], my1 as [y1|], my2 as [y2|]; intros Hx Hx';
      try (by exfalso; inversion Hx'; auto).
    + destruct (cmra_extend n x y1 y2) as ([z1 z2]&?&?&?); auto.
      { by inversion_clear Hx'. }
      by exists (Some z1, Some z2); repeat constructor.
    + by exists (Some x,None); inversion Hx'; repeat constructor.
    + by exists (None,Some x); inversion Hx'; repeat constructor.
    + exists (None,None); repeat constructor.
119
Qed.
120
Canonical Structure optionRA := CMRAT option_cofe_mixin option_cmra_mixin.
121
122
Global Instance option_cmra_identity : CMRAIdentity optionRA.
Proof. split. done. by intros []. by inversion_clear 1. Qed.
123
124
Global Instance option_cmra_discrete : CMRADiscrete A  CMRADiscrete optionRA.
Proof. split; [apply _|]. by intros [x|]; [apply (cmra_discrete_valid x)|]. Qed.
125

126
(** Misc *)
Robbert Krebbers's avatar
Robbert Krebbers committed
127
128
Global Instance Some_cmra_monotone : CMRAMonotone Some.
Proof. split; [apply _|done|intros x y [z ->]; by exists (Some z)]. Qed.
129
130
131
132
Lemma op_is_Some mx my : is_Some (mx  my)  is_Some mx  is_Some my.
Proof.
  destruct mx, my; rewrite /op /option_op /= -!not_eq_None_Some; naive_solver.
Qed.
133
Lemma option_op_positive_dist_l n mx my : mx  my {n} None  mx {n} None.
134
Proof. by destruct mx, my; inversion_clear 1. Qed.
135
Lemma option_op_positive_dist_r n mx my : mx  my {n} None  my {n} None.
136
137
Proof. by destruct mx, my; inversion_clear 1. Qed.

138
139
140
141
142
(** Internalized properties *)
Lemma option_equivI {M} (x y : option A) :
  (x  y)%I  (match x, y with
               | Some a, Some b => a  b | None, None => True | _, _ => False
               end : uPred M)%I.
143
144
145
Proof.
  uPred.unseal. do 2 split. by destruct 1. by destruct x, y; try constructor.
Qed.
146
147
Lemma option_validI {M} (x : option A) :
  ( x)%I  (match x with Some a =>  a | None => True end : uPred M)%I.
148
Proof. uPred.unseal. by destruct x. Qed.
149
150

(** Updates *)
151
Lemma option_updateP (P : A  Prop) (Q : option A  Prop) x :
152
  x ~~>: P  ( y, P y  Q (Some y))  Some x ~~>: Q.
153
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
154
155
156
  intros Hx Hy n [y|] ?.
  { destruct (Hx n y) as (y'&?&?); auto. exists (Some y'); auto. }
  destruct (Hx n (unit x)) as (y'&?&?); rewrite ?cmra_unit_r; auto.
157
158
  by exists (Some y'); split; [auto|apply cmra_validN_op_l with (unit x)].
Qed.
159
Lemma option_updateP' (P : A  Prop) x :
160
  x ~~>: P  Some x ~~>: λ y, default False y P.
161
Proof. eauto using option_updateP. Qed.
162
Lemma option_update x y : x ~~> y  Some x ~~> Some y.
Robbert Krebbers's avatar
Robbert Krebbers committed
163
Proof.
164
  rewrite !cmra_update_updateP; eauto using option_updateP with congruence.
Robbert Krebbers's avatar
Robbert Krebbers committed
165
Qed.
166
167
Lemma option_update_None `{Empty A, !CMRAIdentity A} :  ~~> Some .
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
168
  intros n [x|] ?; rewrite /op /cmra_op /validN /cmra_validN /= ?left_id;
169
    auto using cmra_empty_validN.
170
Qed.
171
172
173
End cmra.
Arguments optionRA : clear implicits.

174
175
176
177
(** Functor *)
Instance option_fmap_ne {A B : cofeT} (f : A  B) n:
  Proper (dist n ==> dist n) f  Proper (dist n==>dist n) (fmap (M:=option) f).
Proof. by intros Hf; destruct 1; constructor; apply Hf. Qed.
178
179
Instance option_fmap_cmra_monotone {A B : cmraT} (f: A  B) `{!CMRAMonotone f} :
  CMRAMonotone (fmap f : option A  option B).
180
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
181
  split; first apply _.
182
  - intros n [x|] ?; rewrite /cmra_validN //=. by apply (validN_preserving f).
Robbert Krebbers's avatar
Robbert Krebbers committed
183
184
  - intros mx my; rewrite !option_included.
    intros [->|(x&y&->&->&?)]; simpl; eauto 10 using @included_preserving.
185
Qed.
186
187
188
189
Definition optionC_map {A B} (f : A -n> B) : optionC A -n> optionC B :=
  CofeMor (fmap f : optionC A  optionC B).
Instance optionC_map_ne A B n : Proper (dist n ==> dist n) (@optionC_map A B).
Proof. by intros f f' Hf []; constructor; apply Hf. Qed.
Ralf Jung's avatar
Ralf Jung committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

Program Definition optionF (Σ : iFunctor) : iFunctor := {|
  ifunctor_car := optionRA  Σ; ifunctor_map A B := optionC_map  ifunctor_map Σ
|}.
Next Obligation.
  by intros Σ A B n f g Hfg; apply optionC_map_ne, ifunctor_map_ne.
Qed.
Next Obligation.
  intros Σ A x. rewrite /= -{2}(option_fmap_id x).
  apply option_fmap_setoid_ext=>y; apply ifunctor_map_id.
Qed.
Next Obligation.
  intros Σ A B C f g x. rewrite /= -option_fmap_compose.
  apply option_fmap_setoid_ext=>y; apply ifunctor_map_compose.
Qed.