iprod.v 13.4 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 7
(** Need to put this in a definition to make canonical structures to work. *)
Definition iprod {A} (B : A  cofeT) :=  x, B x.
Robbert Krebbers's avatar
Robbert Krebbers committed
8
Definition iprod_insert {A} {B : A  cofeT} `{ x x' : A, Decision (x = x')}
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 12 13 14
Global Instance iprod_empty {A} {B : A  cofeT}
  `{ x, Empty (B x)} : Empty (iprod B) := λ x, .
Definition iprod_singleton {A} {B : A  cofeT} 
    `{ x x' : A, Decision (x = x'),  x : A, Empty (B x)}
15
  (x : A) (y : B x) : iprod B := iprod_insert x y .
Robbert Krebbers's avatar
Robbert Krebbers committed
16 17
Instance: Params (@iprod_insert) 4.
Instance: Params (@iprod_singleton) 5.
18 19 20 21 22

Section iprod_cofe.
  Context {A} {B : A  cofeT}.
  Implicit Types x : A.
  Implicit Types f g : iprod B.
23

24
  Instance iprod_equiv : Equiv (iprod B) := λ f g,  x, f x  g x.
25
  Instance iprod_dist : Dist (iprod B) := λ n f g,  x, f x {n} g x.
26 27 28 29 30 31 32 33
  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.
34
    - intros f g; split; [intros Hfg n k; apply equiv_dist, Hfg|].
35
      intros Hfg k; apply equiv_dist; intros n; apply Hfg.
36
    - intros n; split.
37 38
      + by intros f x.
      + by intros f g ? x.
39
      + by intros f g h ?? x; trans (g x).
40
    - intros n f g Hfg x; apply dist_S, Hfg.
Robbert Krebbers's avatar
Robbert Krebbers committed
41 42
    - intros n c x.
      rewrite /compl /iprod_compl (conv_compl n (iprod_chain c x)).
43 44 45 46
      apply (chain_cauchy c); lia.
  Qed.
  Canonical Structure iprodC : cofeT := CofeT iprod_cofe_mixin.

47 48 49 50
  (** Properties of empty *)
  Section empty.
    Context `{ x, Empty (B x)}.
    Definition iprod_lookup_empty  x :  x =  := eq_refl.
51
    Global Instance iprod_empty_timeless :
52
      ( x : A, Timeless ( : B x))  Timeless ( : iprod B).
53
    Proof. intros ? f Hf x. by apply: timeless. Qed.
54 55
  End empty.

56
  (** Properties of iprod_insert. *)
57
  Context `{ x x' : A, Decision (x = x')}.
58

Robbert Krebbers's avatar
Robbert Krebbers committed
59
  Global Instance iprod_insert_ne n x :
60 61 62 63 64 65 66
    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 _.
67

68 69 70 71 72 73 74 75 76
  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.

77
  Global Instance iprod_lookup_timeless f x : Timeless f  Timeless (f x).
78
  Proof.
79
    intros ? y ?.
80
    cut (f  iprod_insert x y f).
81
    { by move=> /(_ x)->; rewrite iprod_lookup_insert. }
82
    by apply: timeless=>x'; destruct (decide (x = x')) as [->|];
83
      rewrite ?iprod_lookup_insert ?iprod_lookup_insert_ne.
84 85 86 87
  Qed.
  Global Instance iprod_insert_timeless f x y :
    Timeless f  Timeless y  Timeless (iprod_insert x y f).
  Proof.
88
    intros ?? g Heq x'; destruct (decide (x = x')) as [->|].
89
    - rewrite iprod_lookup_insert.
90
      apply: timeless. by rewrite -(Heq x') iprod_lookup_insert.
91
    - rewrite iprod_lookup_insert_ne //.
92
      apply: timeless. by rewrite -(Heq x') iprod_lookup_insert_ne.
93 94 95
  Qed.

  (** Properties of iprod_singletom. *)
96
  Context `{ x : A, Empty (B x)}.
97

Robbert Krebbers's avatar
Robbert Krebbers committed
98
  Global Instance iprod_singleton_ne n x :
99 100 101 102
    Proper (dist n ==> dist n) (iprod_singleton x).
  Proof. by intros y1 y2 Hy; rewrite /iprod_singleton Hy. Qed.
  Global Instance iprod_singleton_proper x :
    Proper (() ==> ()) (iprod_singleton x) := ne_proper _.
103

104 105
  Lemma iprod_lookup_singleton x y : (iprod_singleton x y) x = y.
  Proof. by rewrite /iprod_singleton iprod_lookup_insert. Qed.
106
  Lemma iprod_lookup_singleton_ne x x' y: x  x'  (iprod_singleton x y) x' = .
107
  Proof. intros; by rewrite /iprod_singleton iprod_lookup_insert_ne. Qed.
108 109

  Global Instance iprod_singleton_timeless x (y : B x) :
110 111
    ( x : A, Timeless ( : B x))  Timeless y  Timeless (iprod_singleton x y).
  Proof. eauto using iprod_insert_timeless, iprod_empty_timeless. Qed.
112 113 114 115 116
End iprod_cofe.

Arguments iprodC {_} _.

Section iprod_cmra.
Robbert Krebbers's avatar
Robbert Krebbers committed
117
  Context `{Finite A} {B : A  cmraT}.
118
  Implicit Types f g : iprod B.
119

120
  Instance iprod_op : Op (iprod B) := λ f g x, f x  g x.
Ralf Jung's avatar
Ralf Jung committed
121
  Instance iprod_core : Core (iprod B) := λ f x, core (f x).
122
  Instance iprod_valid : Valid (iprod B) := λ f,  x,  f x.
123
  Instance iprod_validN : ValidN (iprod B) := λ n f,  x, {n} f x.
124 125

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

Robbert Krebbers's avatar
Robbert Krebbers committed
128
  Lemma iprod_included_spec (f g : iprod B) : f  g   x, f x  g x.
129
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
130 131
    split; [by intros [h Hh] x; exists (h x); rewrite /op /iprod_op (Hh x)|].
    intros [h ?]%finite_choice. by exists h.
132
  Qed.
133

134 135 136
  Definition iprod_cmra_mixin : CMRAMixin (iprod B).
  Proof.
    split.
137
    - by intros n f1 f2 f3 Hf x; rewrite iprod_lookup_op (Hf x).
Ralf Jung's avatar
Ralf Jung committed
138
    - by intros n f1 f2 Hf x; rewrite iprod_lookup_core (Hf x).
139
    - by intros n f1 f2 Hf ? x; rewrite -(Hf x).
140 141 142
    - intros g; split.
      + intros Hg n i; apply cmra_valid_validN, Hg.
      + intros Hg i; apply cmra_valid_validN=> n; apply Hg.
143 144 145
    - 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
146 147
    - 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
148
    - intros f1 f2; rewrite !iprod_included_spec=> Hf x.
Ralf Jung's avatar
Ralf Jung committed
149
      by rewrite iprod_lookup_core; apply cmra_core_preserving, Hf.
150
    - intros n f1 f2 Hf x; apply cmra_validN_op_l with (f2 x), Hf.
151 152 153 154
    - 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)).
155
  Qed.
156
  Canonical Structure iprodR : cmraT := CMRAT iprod_cofe_mixin iprod_cmra_mixin.
Ralf Jung's avatar
Ralf Jung committed
157 158
  Global Instance iprod_cmra_unit `{ x, Empty (B x)} :
    ( x, CMRAUnit (B x))  CMRAUnit iprodR.
159 160
  Proof.
    intros ?; split.
Ralf Jung's avatar
Ralf Jung committed
161
    - intros x; apply cmra_unit_valid.
162 163
    - by intros f x; rewrite iprod_lookup_op left_id.
    - by apply _.
164 165
  Qed.

166
  (** Internalized properties *)
167
  Lemma iprod_equivI {M} g1 g2 : (g1  g2)  ( i, g1 i  g2 i : uPred M).
168
  Proof. by uPred.unseal. Qed.
169
  Lemma iprod_validI {M} g : ( g)  ( i,  g i : uPred M).
170
  Proof. by uPred.unseal. Qed.
171

172
  (** Properties of iprod_insert. *)
173
  Context `{ x x' : A, Decision (x = x')}.
174

175 176 177 178
  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
179
    intros Hy1 HP n gf Hg. destruct (Hy1 n (gf x)) as (y2&?&?).
180 181 182 183 184 185 186 187 188 189 190 191 192 193
    { 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.
194
    rewrite !cmra_update_updateP; eauto using iprod_insert_updateP with subst.
195 196
  Qed.

197
  (** Properties of iprod_singleton. *)
Ralf Jung's avatar
Ralf Jung committed
198
  Context `{ x, Empty (B x),  x, CMRAUnit (B x)}.
199

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

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

216 217 218 219
  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 [->|].
220 221
    - by rewrite iprod_lookup_op !iprod_lookup_singleton.
    - by rewrite iprod_lookup_op !iprod_lookup_singleton_ne // left_id.
222 223 224 225 226 227 228 229
  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 
230
    iprod_singleton x y1 ~~>: λ g,  y2, g = iprod_singleton x y2  P y2.
231
  Proof. eauto using iprod_singleton_updateP. Qed.
232 233 234
  Lemma iprod_singleton_update x y1 y2 :
    y1 ~~> y2  iprod_singleton x y1 ~~> iprod_singleton x y2.
  Proof. eauto using iprod_insert_update. Qed.
235 236

  Lemma iprod_singleton_updateP_empty x (P : B x  Prop) (Q : iprod B  Prop) :
237
     ~~>: P  ( y2, P y2  Q (iprod_singleton x y2))   ~~>: Q.
238
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
239
    intros Hx HQ n gf Hg. destruct (Hx n (gf x)) as (y2&?&?); first apply Hg.
240 241
    exists (iprod_singleton x y2); split; [by apply HQ|].
    intros x'; destruct (decide (x' = x)) as [->|].
242 243
    - by rewrite iprod_lookup_op iprod_lookup_singleton.
    - rewrite iprod_lookup_op iprod_lookup_singleton_ne //. apply Hg.
244
  Qed.
245 246 247
  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.
248 249
End iprod_cmra.

Robbert Krebbers's avatar
Robbert Krebbers committed
250
Arguments iprodR {_ _ _} _.
251

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
(** * Functor *)
Definition iprod_map {A} {B1 B2 : A  cofeT} (f :  x, B1 x  B2 x)
  (g : iprod B1) : iprod B2 := λ x, f _ (g x).

Lemma iprod_map_ext {A} {B1 B2 : A  cofeT} (f1 f2 :  x, B1 x  B2 x) g :
  ( x, f1 x (g x)  f2 x (g x))  iprod_map f1 g  iprod_map f2 g.
Proof. done. Qed.
Lemma iprod_map_id {A} {B: A  cofeT} (g : iprod B) : iprod_map (λ _, id) g = g.
Proof. done. Qed.
Lemma iprod_map_compose {A} {B1 B2 B3 : A  cofeT}
    (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.

Instance iprod_map_ne {A} {B1 B2 : A  cofeT} (f :  x, B1 x  B2 x) n :
  ( 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.
Robbert Krebbers's avatar
Robbert Krebbers committed
270 271
Instance iprod_map_cmra_monotone `{Finite A}
    {B1 B2: A  cmraT} (f :  x, B1 x  B2 x) :
272 273
  ( x, CMRAMonotone (f x))  CMRAMonotone (iprod_map f).
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
274
  split; first apply _.
275
  - intros n g Hg x; rewrite /iprod_map; apply (validN_preserving (f _)), Hg.
Robbert Krebbers's avatar
Robbert Krebbers committed
276
  - intros g1 g2; rewrite !iprod_included_spec=> Hf x.
277
    rewrite /iprod_map; apply (included_preserving _), Hf.
278 279
Qed.

280 281 282 283 284 285
Definition iprodC_map {A} {B1 B2 : A  cofeT} (f : iprod (λ x, B1 x -n> B2 x)) :
  iprodC B1 -n> iprodC B2 := CofeMor (iprod_map f).
Instance iprodC_map_ne {A} {B1 B2 : A  cofeT} n :
  Proper (dist n ==> dist n) (@iprodC_map A B1 B2).
Proof. intros f1 f2 Hf g x; apply Hf. Qed.

286 287 288
Program Definition iprodCF {C} (F : C  cFunctor) : cFunctor := {|
  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)
289
|}.
290 291 292
Next Obligation.
  intros C F A1 A2 B1 B2 n ?? g. by apply iprodC_map_ne=>?; apply cFunctor_ne.
Qed.
293
Next Obligation.
294 295
  intros C F A B g; simpl. rewrite -{2}(iprod_map_id g).
  apply iprod_map_ext=> y; apply cFunctor_id.
296 297
Qed.
Next Obligation.
298 299 300
  intros C F A1 A2 A3 B1 B2 B3 f1 f2 f1' f2' g. rewrite /= -iprod_map_compose.
  apply iprod_map_ext=>y; apply cFunctor_compose.
Qed.
301 302 303 304 305 306 307
Instance iprodCF_contractive {C} (F : C  cFunctor) :
  ( 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.

Robbert Krebbers's avatar
Robbert Krebbers committed
308
Program Definition iprodRF `{Finite C} (F : C  rFunctor) : rFunctor := {|
309 310 311
  rFunctor_car A B := iprodR (λ c, rFunctor_car (F c) A B);
  rFunctor_map A1 A2 B1 B2 fg := iprodC_map (λ c, rFunctor_map (F c) fg)
|}.
312
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
313 314
  intros C ?? F A1 A2 B1 B2 n ?? g.
  by apply iprodC_map_ne=>?; apply rFunctor_ne.
315
Qed.
316
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
317
  intros C ?? F A B g; simpl. rewrite -{2}(iprod_map_id g).
318 319 320
  apply iprod_map_ext=> y; apply rFunctor_id.
Qed.
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
321
  intros C ?? F A1 A2 A3 B1 B2 B3 f1 f2 f1' f2' g. rewrite /=-iprod_map_compose.
322
  apply iprod_map_ext=>y; apply rFunctor_compose.
323
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
324
Instance iprodRF_contractive `{Finite C} (F : C  rFunctor) :
325 326 327 328 329
  ( c, rFunctorContractive (F c))  rFunctorContractive (iprodRF F).
Proof.
  intros ? A1 A2 B1 B2 n ?? g.
  by apply iprodC_map_ne=>c; apply rFunctor_contractive.
Qed.