iprod.v 13.7 KB
Newer Older
1 2
From iris.algebra Require Export cmra.
From iris.algebra Require Import upred.
Robbert Krebbers's avatar
Robbert Krebbers committed
3
From iris.prelude Require Import finite.
4

5
(** * Indexed product *)
6
(** Need to put this in a definition to make canonical structures to work. *)
7 8
Definition iprod `{Finite A} (B : A  cofeT) :=  x, B x.
Definition iprod_insert `{Finite A} {B : A  cofeT}
9 10
    (x : A) (y : B x) (f : iprod B) : iprod B := λ x',
  match decide (x = x') with left H => eq_rect _ B y _ H | right _ => f x' end.
11
Instance: Params (@iprod_insert) 5.
12 13

Section iprod_cofe.
14
  Context `{Finite A} {B : A  cofeT}.
15 16
  Implicit Types x : A.
  Implicit Types f g : iprod B.
17

18
  Instance iprod_equiv : Equiv (iprod B) := λ f g,  x, f x  g x.
19
  Instance iprod_dist : Dist (iprod B) := λ n f g,  x, f x {n} g x.
20 21 22 23 24 25 26 27
  Program Definition iprod_chain (c : chain (iprod B)) (x : A) : chain (B x) :=
    {| chain_car n := c n x |}.
  Next Obligation. by intros c x n i ?; apply (chain_cauchy c). Qed.
  Program Instance iprod_compl : Compl (iprod B) := λ c x,
    compl (iprod_chain c x).
  Definition iprod_cofe_mixin : CofeMixin (iprod B).
  Proof.
    split.
28
    - intros f g; split; [intros Hfg n k; apply equiv_dist, Hfg|].
29
      intros Hfg k; apply equiv_dist; intros n; apply Hfg.
30
    - intros n; split.
31 32
      + by intros f x.
      + by intros f g ? x.
33
      + by intros f g h ?? x; trans (g x).
34
    - intros n f g Hfg x; apply dist_S, Hfg.
Robbert Krebbers's avatar
Robbert Krebbers committed
35 36
    - intros n c x.
      rewrite /compl /iprod_compl (conv_compl n (iprod_chain c x)).
37 38
      apply (chain_cauchy c); lia.
  Qed.
39
  Canonical Structure iprodC : cofeT := CofeT (iprod B) iprod_cofe_mixin.
40

41
  (** Properties of iprod_insert. *)
42
  Context `{ x x' : A, Decision (x = x')}.
43

Robbert Krebbers's avatar
Robbert Krebbers committed
44
  Global Instance iprod_insert_ne n x :
45 46 47 48 49 50 51
    Proper (dist n ==> dist n ==> dist n) (iprod_insert x).
  Proof.
    intros y1 y2 ? f1 f2 ? x'; rewrite /iprod_insert.
    by destruct (decide _) as [[]|].
  Qed.
  Global Instance iprod_insert_proper x :
    Proper (() ==> () ==> ()) (iprod_insert x) := ne_proper_2 _.
52

53 54 55 56 57 58 59 60 61
  Lemma iprod_lookup_insert f x y : (iprod_insert x y f) x = y.
  Proof.
    rewrite /iprod_insert; destruct (decide _) as [Hx|]; last done.
    by rewrite (proof_irrel Hx eq_refl).
  Qed.
  Lemma iprod_lookup_insert_ne f x x' y :
    x  x'  (iprod_insert x y f) x' = f x'.
  Proof. by rewrite /iprod_insert; destruct (decide _). Qed.

62
  Global Instance iprod_lookup_timeless f x : Timeless f  Timeless (f x).
63
  Proof.
64
    intros ? y ?.
65
    cut (f  iprod_insert x y f).
66
    { by move=> /(_ x)->; rewrite iprod_lookup_insert. }
67 68
    apply (timeless _)=> x'; destruct (decide (x = x')) as [->|];
      by rewrite ?iprod_lookup_insert ?iprod_lookup_insert_ne.
69 70 71 72
  Qed.
  Global Instance iprod_insert_timeless f x y :
    Timeless f  Timeless y  Timeless (iprod_insert x y f).
  Proof.
73
    intros ?? g Heq x'; destruct (decide (x = x')) as [->|].
74
    - rewrite iprod_lookup_insert.
75
      apply: timeless. by rewrite -(Heq x') iprod_lookup_insert.
76
    - rewrite iprod_lookup_insert_ne //.
77
      apply: timeless. by rewrite -(Heq x') iprod_lookup_insert_ne.
78
  Qed.
79 80
End iprod_cofe.

81
Arguments iprodC {_ _ _} _.
82 83

Section iprod_cmra.
84
  Context `{Finite A} {B : A  ucmraT}.
85
  Implicit Types f g : iprod B.
86

87
  Instance iprod_op : Op (iprod B) := λ f g x, f x  g x.
Robbert Krebbers's avatar
Robbert Krebbers committed
88
  Instance iprod_pcore : PCore (iprod B) := λ f, Some (λ x, core (f x)).
89
  Instance iprod_valid : Valid (iprod B) := λ f,  x,  f x.
90
  Instance iprod_validN : ValidN (iprod B) := λ n f,  x, {n} f x.
91 92

  Definition iprod_lookup_op f g x : (f  g) x = f x  g x := eq_refl.
Ralf Jung's avatar
Ralf Jung committed
93
  Definition iprod_lookup_core f x : (core f) x = core (f x) := eq_refl.
94

Robbert Krebbers's avatar
Robbert Krebbers committed
95
  Lemma iprod_included_spec (f g : iprod B) : f  g   x, f x  g x.
96
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
97 98
    split; [by intros [h Hh] x; exists (h x); rewrite /op /iprod_op (Hh x)|].
    intros [h ?]%finite_choice. by exists h.
99
  Qed.
100

101
  Lemma iprod_cmra_mixin : CMRAMixin (iprod B).
102
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
103 104
    apply cmra_total_mixin.
    - eauto.
105
    - by intros n f1 f2 f3 Hf x; rewrite iprod_lookup_op (Hf x).
Ralf Jung's avatar
Ralf Jung committed
106
    - by intros n f1 f2 Hf x; rewrite iprod_lookup_core (Hf x).
107
    - by intros n f1 f2 Hf ? x; rewrite -(Hf x).
108 109 110
    - intros g; split.
      + intros Hg n i; apply cmra_valid_validN, Hg.
      + intros Hg i; apply cmra_valid_validN=> n; apply Hg.
111 112 113
    - intros n f Hf x; apply cmra_validN_S, Hf.
    - by intros f1 f2 f3 x; rewrite iprod_lookup_op assoc.
    - by intros f1 f2 x; rewrite iprod_lookup_op comm.
Ralf Jung's avatar
Ralf Jung committed
114 115
    - by intros f x; rewrite iprod_lookup_op iprod_lookup_core cmra_core_l.
    - by intros f x; rewrite iprod_lookup_core cmra_core_idemp.
Robbert Krebbers's avatar
Robbert Krebbers committed
116
    - intros f1 f2; rewrite !iprod_included_spec=> Hf x.
Ralf Jung's avatar
Ralf Jung committed
117
      by rewrite iprod_lookup_core; apply cmra_core_preserving, Hf.
118
    - intros n f1 f2 Hf x; apply cmra_validN_op_l with (f2 x), Hf.
119 120 121 122
    - intros n f f1 f2 Hf Hf12.
      set (g x := cmra_extend n (f x) (f1 x) (f2 x) (Hf x) (Hf12 x)).
      exists ((λ x, (proj1_sig (g x)).1), (λ x, (proj1_sig (g x)).2)).
      split_and?; intros x; apply (proj2_sig (g x)).
123
  Qed.
124
  Canonical Structure iprodR :=
125
    CMRAT (iprod B) iprod_cofe_mixin iprod_cmra_mixin.
126 127 128 129 130

  Instance iprod_empty : Empty (iprod B) := λ x, .
  Definition iprod_lookup_empty x :  x =  := eq_refl.

  Lemma iprod_ucmra_mixin : UCMRAMixin (iprod B).
131
  Proof.
132 133
    split.
    - intros x; apply ucmra_unit_valid.
134
    - by intros f x; rewrite iprod_lookup_op left_id.
135
    - intros f Hf x. by apply: timeless.
Robbert Krebbers's avatar
Robbert Krebbers committed
136
    - constructor=> x. apply persistent_core, _.
137
  Qed.
138 139
  Canonical Structure iprodUR :=
    UCMRAT (iprod B) iprod_cofe_mixin iprod_cmra_mixin iprod_ucmra_mixin.
140

141
  (** Internalized properties *)
142
  Lemma iprod_equivI {M} g1 g2 : (g1  g2)  ( i, g1 i  g2 i : uPred M).
143
  Proof. by uPred.unseal. Qed.
144
  Lemma iprod_validI {M} g : ( g)  ( i,  g i : uPred M).
145
  Proof. by uPred.unseal. Qed.
146

147
  (** Properties of iprod_insert. *)
148 149 150 151
  Lemma iprod_insert_updateP x (P : B x  Prop) (Q : iprod B  Prop) g y1 :
    y1 ~~>: P  ( y2, P y2  Q (iprod_insert x y2 g)) 
    iprod_insert x y1 g ~~>: Q.
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
152 153
    intros Hy1 HP; apply cmra_total_updateP.
    intros n gf Hg. destruct (Hy1 n (Some (gf x))) as (y2&?&?).
154 155 156 157 158 159 160 161 162 163 164 165 166 167
    { move: (Hg x). by rewrite iprod_lookup_op iprod_lookup_insert. }
    exists (iprod_insert x y2 g); split; [auto|].
    intros x'; destruct (decide (x' = x)) as [->|];
      rewrite iprod_lookup_op ?iprod_lookup_insert //; [].
    move: (Hg x'). by rewrite iprod_lookup_op !iprod_lookup_insert_ne.
  Qed.

  Lemma iprod_insert_updateP' x (P : B x  Prop) g y1 :
    y1 ~~>: P 
    iprod_insert x y1 g ~~>: λ g',  y2, g' = iprod_insert x y2 g  P y2.
  Proof. eauto using iprod_insert_updateP. Qed.
  Lemma iprod_insert_update g x y1 y2 :
    y1 ~~> y2  iprod_insert x y1 g ~~> iprod_insert x y2 g.
  Proof.
168
    rewrite !cmra_update_updateP; eauto using iprod_insert_updateP with subst.
169
  Qed.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
End iprod_cmra.

Arguments iprodR {_ _ _} _.
Arguments iprodUR {_ _ _} _.

Definition iprod_singleton `{Finite A} {B : A  ucmraT} 
  (x : A) (y : B x) : iprod B := iprod_insert x y .
Instance: Params (@iprod_singleton) 5.

Section iprod_singleton.
  Context `{Finite A} {B : A  ucmraT}.
  Implicit Types x : A.

  Global Instance iprod_singleton_ne n x :
    Proper (dist n ==> dist n) (iprod_singleton x : B x  _).
  Proof. intros y1 y2 ?; apply iprod_insert_ne. done. by apply equiv_dist. Qed.
  Global Instance iprod_singleton_proper x :
    Proper (() ==> ()) (iprod_singleton x) := ne_proper _.
188

189 190 191 192 193
  Lemma iprod_lookup_singleton x (y : B x) : (iprod_singleton x y) x = y.
  Proof. by rewrite /iprod_singleton iprod_lookup_insert. Qed.
  Lemma iprod_lookup_singleton_ne x x' (y : B x) :
    x  x'  (iprod_singleton x y) x' = .
  Proof. intros; by rewrite /iprod_singleton iprod_lookup_insert_ne. Qed.
194

195 196 197 198
  Global Instance iprod_singleton_timeless x (y : B x) :
    Timeless y  Timeless (iprod_singleton x y) := _.

  Lemma iprod_singleton_validN n x (y : B x) : {n} iprod_singleton x y  {n} y.
199
  Proof.
200 201 202
    split; [by move=>/(_ x); rewrite iprod_lookup_singleton|].
    move=>Hx x'; destruct (decide (x = x')) as [->|];
      rewrite ?iprod_lookup_singleton ?iprod_lookup_singleton_ne //.
203
    by apply ucmra_unit_validN.
204 205
  Qed.

Ralf Jung's avatar
Ralf Jung committed
206 207
  Lemma iprod_core_singleton x (y : B x) :
    core (iprod_singleton x y)  iprod_singleton x (core y).
208
  Proof.
209 210
    move=>x'; destruct (decide (x = x')) as [->|];
      by rewrite iprod_lookup_core ?iprod_lookup_singleton
Robbert Krebbers's avatar
Robbert Krebbers committed
211
      ?iprod_lookup_singleton_ne // (persistent_core ).
212 213
  Qed.

214 215
  Global Instance iprod_singleton_persistent x (y : B x) :
    Persistent y  Persistent (iprod_singleton x y).
Robbert Krebbers's avatar
Robbert Krebbers committed
216
  Proof. by rewrite !persistent_total iprod_core_singleton=> ->. Qed.
217

218 219 220 221
  Lemma iprod_op_singleton (x : A) (y1 y2 : B x) :
    iprod_singleton x y1  iprod_singleton x y2  iprod_singleton x (y1  y2).
  Proof.
    intros x'; destruct (decide (x' = x)) as [->|].
222 223
    - by rewrite iprod_lookup_op !iprod_lookup_singleton.
    - by rewrite iprod_lookup_op !iprod_lookup_singleton_ne // left_id.
224 225 226 227 228 229 230 231
  Qed.

  Lemma iprod_singleton_updateP x (P : B x  Prop) (Q : iprod B  Prop) y1 :
    y1 ~~>: P  ( y2, P y2  Q (iprod_singleton x y2)) 
    iprod_singleton x y1 ~~>: Q.
  Proof. rewrite /iprod_singleton; eauto using iprod_insert_updateP. Qed.
  Lemma iprod_singleton_updateP' x (P : B x  Prop) y1 :
    y1 ~~>: P 
232
    iprod_singleton x y1 ~~>: λ g,  y2, g = iprod_singleton x y2  P y2.
233
  Proof. eauto using iprod_singleton_updateP. Qed.
234
  Lemma iprod_singleton_update x (y1 y2 : B x) :
235 236
    y1 ~~> y2  iprod_singleton x y1 ~~> iprod_singleton x y2.
  Proof. eauto using iprod_insert_update. Qed.
237 238

  Lemma iprod_singleton_updateP_empty x (P : B x  Prop) (Q : iprod B  Prop) :
239
     ~~>: P  ( y2, P y2  Q (iprod_singleton x y2))   ~~>: Q.
240
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
241 242
    intros Hx HQ; apply cmra_total_updateP.
    intros n gf Hg. destruct (Hx n (Some (gf x))) as (y2&?&?); first apply Hg.
243 244
    exists (iprod_singleton x y2); split; [by apply HQ|].
    intros x'; destruct (decide (x' = x)) as [->|].
245 246
    - by rewrite iprod_lookup_op iprod_lookup_singleton.
    - rewrite iprod_lookup_op iprod_lookup_singleton_ne //. apply Hg.
247
  Qed.
248 249 250
  Lemma iprod_singleton_updateP_empty' x (P : B x  Prop) :
     ~~>: P   ~~>: λ g,  y2, g = iprod_singleton x y2  P y2.
  Proof. eauto using iprod_singleton_updateP_empty. Qed.
251 252 253 254 255 256 257
  Lemma iprod_singleton_update_empty x (y : B x) :
     ~~> y   ~~> iprod_singleton x y.
  Proof.
    rewrite !cmra_update_updateP;
      eauto using iprod_singleton_updateP_empty with subst.
  Qed.
End iprod_singleton.
258

259
(** * Functor *)
260
Definition iprod_map `{Finite A} {B1 B2 : A  cofeT} (f :  x, B1 x  B2 x)
261 262
  (g : iprod B1) : iprod B2 := λ x, f _ (g x).

263
Lemma iprod_map_ext `{Finite A} {B1 B2 : A  cofeT} (f1 f2 :  x, B1 x  B2 x) (g : iprod B1) :
264 265
  ( x, f1 x (g x)  f2 x (g x))  iprod_map f1 g  iprod_map f2 g.
Proof. done. Qed.
266 267
Lemma iprod_map_id `{Finite A} {B : A  cofeT} (g : iprod B) :
  iprod_map (λ _, id) g = g.
268
Proof. done. Qed.
269
Lemma iprod_map_compose `{Finite A} {B1 B2 B3 : A  cofeT}
270 271 272 273
    (f1 :  x, B1 x  B2 x) (f2 :  x, B2 x  B3 x) (g : iprod B1) :
  iprod_map (λ x, f2 x  f1 x) g = iprod_map f2 (iprod_map f1 g).
Proof. done. Qed.

274
Instance iprod_map_ne `{Finite A} {B1 B2 : A  cofeT} (f :  x, B1 x  B2 x) n :
275 276 277
  ( x, Proper (dist n ==> dist n) (f x)) 
  Proper (dist n ==> dist n) (iprod_map f).
Proof. by intros ? y1 y2 Hy x; rewrite /iprod_map (Hy x). Qed.
278 279
Instance iprod_map_cmra_monotone
    `{Finite A} {B1 B2 : A  ucmraT} (f :  x, B1 x  B2 x) :
280 281
  ( x, CMRAMonotone (f x))  CMRAMonotone (iprod_map f).
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
282
  split; first apply _.
283
  - intros n g Hg x; rewrite /iprod_map; apply (validN_preserving (f _)), Hg.
Robbert Krebbers's avatar
Robbert Krebbers committed
284
  - intros g1 g2; rewrite !iprod_included_spec=> Hf x.
285
    rewrite /iprod_map; apply (included_preserving _), Hf.
286 287
Qed.

288 289
Definition iprodC_map `{Finite A} {B1 B2 : A  cofeT}
    (f : iprod (λ x, B1 x -n> B2 x)) :
290
  iprodC B1 -n> iprodC B2 := CofeMor (iprod_map f).
291 292
Instance iprodC_map_ne `{Finite A} {B1 B2 : A  cofeT} n :
  Proper (dist n ==> dist n) (@iprodC_map A _ _ B1 B2).
293 294
Proof. intros f1 f2 Hf g x; apply Hf. Qed.

295
Program Definition iprodCF `{Finite C} (F : C  cFunctor) : cFunctor := {|
296 297
  cFunctor_car A B := iprodC (λ c, cFunctor_car (F c) A B);
  cFunctor_map A1 A2 B1 B2 fg := iprodC_map (λ c, cFunctor_map (F c) fg)
298
|}.
299
Next Obligation.
300
  intros C ?? F A1 A2 B1 B2 n ?? g. by apply iprodC_map_ne=>?; apply cFunctor_ne.
301
Qed.
302
Next Obligation.
303
  intros C ?? F A B g; simpl. rewrite -{2}(iprod_map_id g).
304
  apply iprod_map_ext=> y; apply cFunctor_id.
305 306
Qed.
Next Obligation.
307
  intros C ?? F A1 A2 A3 B1 B2 B3 f1 f2 f1' f2' g. rewrite /= -iprod_map_compose.
308 309
  apply iprod_map_ext=>y; apply cFunctor_compose.
Qed.
310
Instance iprodCF_contractive `{Finite C} (F : C  cFunctor) :
311 312 313 314 315 316
  ( c, cFunctorContractive (F c))  cFunctorContractive (iprodCF F).
Proof.
  intros ? A1 A2 B1 B2 n ?? g.
  by apply iprodC_map_ne=>c; apply cFunctor_contractive.
Qed.

317 318 319
Program Definition iprodURF `{Finite C} (F : C  urFunctor) : urFunctor := {|
  urFunctor_car A B := iprodUR (λ c, urFunctor_car (F c) A B);
  urFunctor_map A1 A2 B1 B2 fg := iprodC_map (λ c, urFunctor_map (F c) fg)
320
|}.
321
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
322
  intros C ?? F A1 A2 B1 B2 n ?? g.
323
  by apply iprodC_map_ne=>?; apply urFunctor_ne.
324
Qed.
325
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
326
  intros C ?? F A B g; simpl. rewrite -{2}(iprod_map_id g).
327
  apply iprod_map_ext=> y; apply urFunctor_id.
328 329
Qed.
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
330
  intros C ?? F A1 A2 A3 B1 B2 B3 f1 f2 f1' f2' g. rewrite /=-iprod_map_compose.
331
  apply iprod_map_ext=>y; apply urFunctor_compose.
332
Qed.
333 334
Instance iprodURF_contractive `{Finite C} (F : C  urFunctor) :
  ( c, urFunctorContractive (F c))  urFunctorContractive (iprodURF F).
335 336
Proof.
  intros ? A1 A2 B1 B2 n ?? g.
337
  by apply iprodC_map_ne=>c; apply urFunctor_contractive.
338
Qed.