list.v 21.5 KB
Newer Older
1
From iris.algebra Require Export cmra.
Ralf Jung's avatar
Ralf Jung committed
2
From stdpp Require Export list.
3 4
From iris.base_logic Require Import base_logic.
From iris.algebra Require Import updates local_updates.
5
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
6 7

Section cofe.
8
Context {A : ofeT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
9 10 11

Instance list_dist : Dist (list A) := λ n, Forall2 (dist n).

12 13 14
Lemma list_dist_lookup n l1 l2 : l1 {n} l2   i, l1 !! i {n} l2 !! i.
Proof. setoid_rewrite dist_option_Forall2. apply Forall2_lookup. Qed.

15 16
Global Instance cons_ne : NonExpansive2 (@cons A) := _.
Global Instance app_ne : NonExpansive2 (@app A) := _.
Robbert Krebbers's avatar
Robbert Krebbers committed
17
Global Instance length_ne n : Proper (dist n ==> (=)) (@length A) := _.
18 19 20 21 22 23
Global Instance tail_ne : NonExpansive (@tail A) := _.
Global Instance take_ne : NonExpansive (@take A n) := _.
Global Instance drop_ne : NonExpansive (@drop A n) := _.
Global Instance list_lookup_ne i :
  NonExpansive (lookup (M:=list A) i).
Proof. intros ????. by apply dist_option_Forall2, Forall2_lookup. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
24 25
Global Instance list_alter_ne n f i :
  Proper (dist n ==> dist n) f 
26
  Proper (dist n ==> dist n) (alter (M:=list A) f i) := _.
27 28 29 30 31 32 33 34
Global Instance list_insert_ne i :
  NonExpansive2 (insert (M:=list A) i) := _.
Global Instance list_inserts_ne i :
  NonExpansive2 (@list_inserts A i) := _.
Global Instance list_delete_ne i :
  NonExpansive (delete (M:=list A) i) := _.
Global Instance option_list_ne : NonExpansive (@option_list A).
Proof. intros ????; by apply Forall2_option_list, dist_option_Forall2. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
35 36 37
Global Instance list_filter_ne n P `{ x, Decision (P x)} :
  Proper (dist n ==> iff) P 
  Proper (dist n ==> dist n) (filter (B:=list A) P) := _.
38 39 40 41 42
Global Instance replicate_ne :
  NonExpansive (@replicate A n) := _.
Global Instance reverse_ne : NonExpansive (@reverse A) := _.
Global Instance last_ne : NonExpansive (@last A).
Proof. intros ????; by apply dist_option_Forall2, Forall2_last. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
43
Global Instance resize_ne n :
44
  NonExpansive2 (@resize A n) := _.
Robbert Krebbers's avatar
Robbert Krebbers committed
45

46 47 48 49 50 51 52 53 54 55
Definition list_ofe_mixin : OfeMixin (list A).
Proof.
  split.
  - intros l k. rewrite equiv_Forall2 -Forall2_forall.
    split; induction 1; constructor; intros; try apply equiv_dist; auto.
  - apply _.
  - rewrite /dist /list_dist. eauto using Forall2_impl, dist_S.
Qed.
Canonical Structure listC := OfeT (list A) list_ofe_mixin.

Robbert Krebbers's avatar
Robbert Krebbers committed
56
Program Definition list_chain
57
    (c : chain listC) (x : A) (k : nat) : chain A :=
58
  {| chain_car n := from_option id x (c n !! k) |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
59
Next Obligation. intros c x k n i ?. by rewrite /= (chain_cauchy c n i). Qed.
60
Definition list_compl `{Cofe A} : Compl listC := λ c,
Robbert Krebbers's avatar
Robbert Krebbers committed
61 62 63 64
  match c 0 with
  | [] => []
  | x :: _ => compl  list_chain c x <$> seq 0 (length (c 0))
  end.
65 66 67 68 69 70 71 72 73 74 75 76 77
Global Program Instance list_cofe `{Cofe A} : Cofe listC :=
  {| compl := list_compl |}.
Next Obligation.
  intros ? n c; rewrite /compl /list_compl.
  destruct (c 0) as [|x l] eqn:Hc0 at 1.
  { by destruct (chain_cauchy c 0 n); auto with omega. }
  rewrite -(λ H, length_ne _ _ _ (chain_cauchy c 0 n H)); last omega.
  apply Forall2_lookup=> i. rewrite -dist_option_Forall2 list_lookup_fmap.
  destruct (decide (i < length (c n))); last first.
  { rewrite lookup_seq_ge ?lookup_ge_None_2; auto with omega. }
  rewrite lookup_seq //= (conv_compl n (list_chain c _ _)) /=.
  destruct (lookup_lt_is_Some_2 (c n) i) as [? Hcn]; first done.
  by rewrite Hcn.
Robbert Krebbers's avatar
Robbert Krebbers committed
78
Qed.
79

Robbert Krebbers's avatar
Robbert Krebbers committed
80 81 82 83 84 85 86 87 88 89 90 91
Global Instance list_discrete : Discrete A  Discrete listC.
Proof. induction 2; constructor; try apply (timeless _); auto. Qed.

Global Instance nil_timeless : Timeless (@nil A).
Proof. inversion_clear 1; constructor. Qed.
Global Instance cons_timeless x l : Timeless x  Timeless l  Timeless (x :: l).
Proof. intros ??; inversion_clear 1; constructor; by apply timeless. Qed.
End cofe.

Arguments listC : clear implicits.

(** Functor *)
92
Lemma list_fmap_ext_ne {A} {B : ofeT} (f g : A  B) (l : list A) n :
93 94
  ( x, f x {n} g x)  f <$> l {n} g <$> l.
Proof. intros Hf. by apply Forall2_fmap, Forall_Forall2, Forall_true. Qed.
95
Instance list_fmap_ne {A B : ofeT} (f : A  B) n:
Robbert Krebbers's avatar
Robbert Krebbers committed
96
  Proper (dist n ==> dist n) f  Proper (dist n ==> dist n) (fmap (M:=list) f).
97
Proof. intros Hf l k ?; by eapply Forall2_fmap, Forall2_impl; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
98 99
Definition listC_map {A B} (f : A -n> B) : listC A -n> listC B :=
  CofeMor (fmap f : listC A  listC B).
100 101
Instance listC_map_ne A B : NonExpansive (@listC_map A B).
Proof. intros n f g ? l. by apply list_fmap_ext_ne. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
102 103 104 105 106 107 108 109 110 111

Program Definition listCF (F : cFunctor) : cFunctor := {|
  cFunctor_car A B := listC (cFunctor_car F A B);
  cFunctor_map A1 A2 B1 B2 fg := listC_map (cFunctor_map F fg)
|}.
Next Obligation.
  by intros F A1 A2 B1 B2 n f g Hfg; apply listC_map_ne, cFunctor_ne.
Qed.
Next Obligation.
  intros F A B x. rewrite /= -{2}(list_fmap_id x).
112
  apply list_fmap_equiv_ext=>y. apply cFunctor_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
113 114 115
Qed.
Next Obligation.
  intros F A1 A2 A3 B1 B2 B3 f g f' g' x. rewrite /= -list_fmap_compose.
116
  apply list_fmap_equiv_ext=>y; apply cFunctor_compose.
Robbert Krebbers's avatar
Robbert Krebbers committed
117 118 119 120 121 122 123
Qed.

Instance listCF_contractive F :
  cFunctorContractive F  cFunctorContractive (listCF F).
Proof.
  by intros ? A1 A2 B1 B2 n f g Hfg; apply listC_map_ne, cFunctor_contractive.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
124 125 126

(* CMRA *)
Section cmra.
127
  Context {A : ucmraT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
128 129 130 131 132 133 134 135 136 137
  Implicit Types l : list A.
  Local Arguments op _ _ !_ !_ / : simpl nomatch.

  Instance list_op : Op (list A) :=
    fix go l1 l2 := let _ : Op _ := @go in
    match l1, l2 with
    | [], _ => l2
    | _, [] => l1
    | x :: l1, y :: l2 => x  y :: l1  l2
    end.
Robbert Krebbers's avatar
Robbert Krebbers committed
138
  Instance list_pcore : PCore (list A) := λ l, Some (core <$> l).
Robbert Krebbers's avatar
Robbert Krebbers committed
139 140 141 142

  Instance list_valid : Valid (list A) := Forall (λ x,  x).
  Instance list_validN : ValidN (list A) := λ n, Forall (λ x, {n} x).

143 144 145 146 147 148 149 150 151
  Lemma cons_valid l x :  (x :: l)   x   l.
  Proof. apply Forall_cons. Qed.
  Lemma cons_validN n l x : {n} (x :: l)  {n} x  {n} l.
  Proof. apply Forall_cons. Qed.
  Lemma app_valid l1 l2 :  (l1 ++ l2)   l1   l2.
  Proof. apply Forall_app. Qed.
  Lemma app_validN n l1 l2 : {n} (l1 ++ l2)  {n} l1  {n} l2.
  Proof. apply Forall_app. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  Lemma list_lookup_valid l :  l   i,  (l !! i).
  Proof.
    rewrite {1}/valid /list_valid Forall_lookup; split.
    - intros Hl i. by destruct (l !! i) as [x|] eqn:?; [apply (Hl i)|].
    - intros Hl i x Hi. move: (Hl i); by rewrite Hi.
  Qed.
  Lemma list_lookup_validN n l : {n} l   i, {n} (l !! i).
  Proof.
    rewrite {1}/validN /list_validN Forall_lookup; split.
    - intros Hl i. by destruct (l !! i) as [x|] eqn:?; [apply (Hl i)|].
    - intros Hl i x Hi. move: (Hl i); by rewrite Hi.
  Qed.
  Lemma list_lookup_op l1 l2 i : (l1  l2) !! i = l1 !! i  l2 !! i.
  Proof.
    revert i l2. induction l1 as [|x l1]; intros [|i] [|y l2];
      by rewrite /= ?left_id_L ?right_id_L.
  Qed.
  Lemma list_lookup_core l i : core l !! i = core (l !! i).
Robbert Krebbers's avatar
Robbert Krebbers committed
170 171 172 173
  Proof.
    rewrite /core /= list_lookup_fmap.
    destruct (l !! i); by rewrite /= ?Some_core.
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

  Lemma list_lookup_included l1 l2 : l1  l2   i, l1 !! i  l2 !! i.
  Proof.
    split.
    { intros [l Hl] i. exists (l !! i). by rewrite Hl list_lookup_op. }
    revert l1. induction l2 as [|y l2 IH]=>-[|x l1] Hl.
    - by exists [].
    - destruct (Hl 0) as [[z|] Hz]; inversion Hz.
    - by exists (y :: l2).
    - destruct (IH l1) as [l3 ?]; first (intros i; apply (Hl (S i))).
      destruct (Hl 0) as [[z|] Hz]; inversion_clear Hz; simplify_eq/=.
      + exists (z :: l3); by constructor.
      + exists (core x :: l3); constructor; by rewrite ?cmra_core_r.
  Qed.

  Definition list_cmra_mixin : CMRAMixin (list A).
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
191 192
    apply cmra_total_mixin.
    - eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
193 194
    - intros n l l1 l2; rewrite !list_dist_lookup=> Hl i.
      by rewrite !list_lookup_op Hl.
Robbert Krebbers's avatar
Robbert Krebbers committed
195
    - intros n l1 l2 Hl; by rewrite /core /= Hl.
Robbert Krebbers's avatar
Robbert Krebbers committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209
    - intros n l1 l2; rewrite !list_dist_lookup !list_lookup_validN=> Hl ? i.
      by rewrite -Hl.
    - intros l. rewrite list_lookup_valid. setoid_rewrite list_lookup_validN.
      setoid_rewrite cmra_valid_validN. naive_solver.
    - intros n x. rewrite !list_lookup_validN. auto using cmra_validN_S.
    - intros l1 l2 l3; rewrite list_equiv_lookup=> i.
      by rewrite !list_lookup_op assoc.
    - intros l1 l2; rewrite list_equiv_lookup=> i.
      by rewrite !list_lookup_op comm.
    - intros l; rewrite list_equiv_lookup=> i.
      by rewrite list_lookup_op list_lookup_core cmra_core_l.
    - intros l; rewrite list_equiv_lookup=> i.
      by rewrite !list_lookup_core cmra_core_idemp.
    - intros l1 l2; rewrite !list_lookup_included=> Hl i.
210
      rewrite !list_lookup_core. by apply cmra_core_mono.
Robbert Krebbers's avatar
Robbert Krebbers committed
211 212
    - intros n l1 l2. rewrite !list_lookup_validN.
      setoid_rewrite list_lookup_op. eauto using cmra_validN_op_l.
213 214 215 216 217 218 219 220
    - intros n l.
      induction l as [|x l IH]=> -[|y1 l1] [|y2 l2] Hl; inversion_clear 1.
      + by exists [], [].
      + exists [], (x :: l); by repeat constructor.
      + exists (x :: l), []; by repeat constructor.
      + inversion_clear Hl. destruct (IH l1 l2) as (l1'&l2'&?&?&?),
          (cmra_extend n x y1 y2) as (y1'&y2'&?&?&?); simplify_eq/=; auto.
        exists (y1' :: l1'), (y2' :: l2'); repeat constructor; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
221
  Qed.
222
  Canonical Structure listR := CMRAT (list A) list_cmra_mixin.
Robbert Krebbers's avatar
Robbert Krebbers committed
223 224

  Global Instance empty_list : Empty (list A) := [].
225
  Definition list_ucmra_mixin : UCMRAMixin (list A).
Robbert Krebbers's avatar
Robbert Krebbers committed
226 227 228 229
  Proof.
    split.
    - constructor.
    - by intros l.
Robbert Krebbers's avatar
Robbert Krebbers committed
230
    - by constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
231
  Qed.
232
  Canonical Structure listUR := UCMRAT (list A) list_ucmra_mixin.
Robbert Krebbers's avatar
Robbert Krebbers committed
233 234 235 236 237 238 239 240 241

  Global Instance list_cmra_discrete : CMRADiscrete A  CMRADiscrete listR.
  Proof.
    split; [apply _|]=> l; rewrite list_lookup_valid list_lookup_validN=> Hl i.
    by apply cmra_discrete_valid.
  Qed.

  Global Instance list_persistent l : ( x : A, Persistent x)  Persistent l.
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
242 243
    intros ?; constructor; apply list_equiv_lookup=> i.
    by rewrite list_lookup_core (persistent_core (l !! i)).
Robbert Krebbers's avatar
Robbert Krebbers committed
244 245 246
  Qed.

  (** Internalized properties *)
247
  Lemma list_equivI {M} l1 l2 : l1  l2 ⊣⊢ ( i, l1 !! i  l2 !! i : uPred M).
Robbert Krebbers's avatar
Robbert Krebbers committed
248
  Proof. uPred.unseal; constructor=> n x ?. apply list_dist_lookup. Qed.
249
  Lemma list_validI {M} l :  l ⊣⊢ ( i,  (l !! i) : uPred M).
Robbert Krebbers's avatar
Robbert Krebbers committed
250 251 252 253
  Proof. uPred.unseal; constructor=> n x ?. apply list_lookup_validN. Qed.
End cmra.

Arguments listR : clear implicits.
254
Arguments listUR : clear implicits.
Robbert Krebbers's avatar
Robbert Krebbers committed
255

256
Instance list_singletonM {A : ucmraT} : SingletonM nat A (list A) := λ n x,
Robbert Krebbers's avatar
Robbert Krebbers committed
257 258 259
  replicate n  ++ [x].

Section properties.
260
  Context {A : ucmraT}.
261
  Implicit Types l : list A.
262
  Implicit Types x y z : A.
Robbert Krebbers's avatar
Robbert Krebbers committed
263 264
  Local Arguments op _ _ !_ !_ / : simpl nomatch.
  Local Arguments cmra_op _ !_ !_ / : simpl nomatch.
265
  Local Arguments ucmra_op _ !_ !_ / : simpl nomatch.
Robbert Krebbers's avatar
Robbert Krebbers committed
266

267
  Lemma list_lookup_opM l mk i : (l ? mk) !! i = l !! i  (mk = (!! i)).
268 269
  Proof. destruct mk; by rewrite /= ?list_lookup_op ?right_id_L. Qed.

270 271 272 273 274
  Global Instance list_op_nil_l : LeftId (=) (@nil A) op.
  Proof. done. Qed.
  Global Instance list_op_nil_r : RightId (=) (@nil A) op.
  Proof. by intros []. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
275
  Lemma list_op_app l1 l2 l3 :
276
    (l1 ++ l3)  l2 = (l1  take (length l1) l2) ++ (l3  drop (length l1) l2).
Robbert Krebbers's avatar
Robbert Krebbers committed
277 278
  Proof.
    revert l2 l3.
279
    induction l1 as [|x1 l1]=> -[|x2 l2] [|x3 l3]; f_equal/=; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
280
  Qed.
281 282 283
  Lemma list_op_app_le l1 l2 l3 :
    length l2  length l1  (l1 ++ l3)  l2 = (l1  l2) ++ l3.
  Proof. intros ?. by rewrite list_op_app take_ge // drop_ge // right_id_L. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
284 285 286 287 288 289 290 291 292 293 294

  Lemma list_lookup_validN_Some n l i x : {n} l  l !! i {n} Some x  {n} x.
  Proof. move=> /list_lookup_validN /(_ i)=> Hl Hi; move: Hl. by rewrite Hi. Qed.
  Lemma list_lookup_valid_Some l i x :  l  l !! i  Some x   x.
  Proof. move=> /list_lookup_valid /(_ i)=> Hl Hi; move: Hl. by rewrite Hi. Qed.

  Lemma list_op_length l1 l2 : length (l1  l2) = max (length l1) (length l2).
  Proof. revert l2. induction l1; intros [|??]; f_equal/=; auto. Qed.

  Lemma replicate_valid n (x : A) :  x   replicate n x.
  Proof. apply Forall_replicate. Qed.
295 296 297
  Global Instance list_singletonM_ne i :
    NonExpansive (@list_singletonM A i).
  Proof. intros n l1 l2 ?. apply Forall2_app; by repeat constructor. Qed.
298 299
  Global Instance list_singletonM_proper i :
    Proper (() ==> ()) (list_singletonM i) := ne_proper _.
Robbert Krebbers's avatar
Robbert Krebbers committed
300

301
  Lemma elem_of_list_singletonM i z x : z  {[i := x]}  z =   z = x.
302 303 304
  Proof.
    rewrite elem_of_app elem_of_list_singleton elem_of_replicate. naive_solver.
  Qed.
305
  Lemma list_lookup_singletonM i x : {[ i := x ]} !! i = Some x.
306
  Proof. induction i; by f_equal/=. Qed.
307 308
  Lemma list_lookup_singletonM_ne i j x :
    i  j  {[ i := x ]} !! j = None  {[ i := x ]} !! j = Some .
309
  Proof. revert j; induction i; intros [|j]; naive_solver auto with omega. Qed.
310
  Lemma list_singletonM_validN n i x : {n} {[ i := x ]}  {n} x.
311 312
  Proof.
    rewrite list_lookup_validN. split.
313
    { move=> /(_ i). by rewrite list_lookup_singletonM. }
314
    intros Hx j; destruct (decide (i = j)); subst.
315 316
    - by rewrite list_lookup_singletonM.
    - destruct (list_lookup_singletonM_ne i j x) as [Hi|Hi]; first done;
317 318
        rewrite Hi; by try apply (ucmra_unit_validN (A:=A)).
  Qed.
319 320 321 322 323
  Lemma list_singleton_valid  i x :  {[ i := x ]}   x.
  Proof.
    rewrite !cmra_valid_validN. by setoid_rewrite list_singletonM_validN.
  Qed.
  Lemma list_singletonM_length i x : length {[ i := x ]} = S i.
324
  Proof.
325
    rewrite /singletonM /list_singletonM app_length replicate_length /=; lia.
326 327
  Qed.

328
  Lemma list_core_singletonM i (x : A) : core {[ i := x ]}  {[ i := core x ]}.
329
  Proof.
330
    rewrite /singletonM /list_singletonM.
Robbert Krebbers's avatar
Robbert Krebbers committed
331
    by rewrite {1}/core /= fmap_app fmap_replicate (persistent_core ).
332
  Qed.
333 334 335 336 337 338 339 340 341 342 343 344 345
  Lemma list_op_singletonM i (x y : A) :
    {[ i := x ]}  {[ i := y ]}  {[ i := x  y ]}.
  Proof.
    rewrite /singletonM /list_singletonM /=.
    induction i; constructor; rewrite ?left_id; auto.
  Qed.
  Lemma list_alter_singletonM f i x : alter f i {[i := x]} = {[i := f x]}.
  Proof.
    rewrite /singletonM /list_singletonM /=. induction i; f_equal/=; auto.
  Qed.
  Global Instance list_singleton_persistent i (x : A) :
    Persistent x  Persistent {[ i := x ]}.
  Proof. by rewrite !persistent_total list_core_singletonM=> ->. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
346 347

  (* Update *)
348 349
  Lemma list_singleton_updateP (P : A  Prop) (Q : list A  Prop) x :
    x ~~>: P  ( y, P y  Q [y])  [x] ~~>: Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
350
  Proof.
351 352 353 354
    rewrite !cmra_total_updateP=> Hup HQ n lf /list_lookup_validN Hv.
    destruct (Hup n (from_option id  (lf !! 0))) as (y&?&Hv').
    { move: (Hv 0). by destruct lf; rewrite /= ?right_id. }
    exists [y]; split; first by auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
355
    apply list_lookup_validN=> i.
356 357 358 359 360 361 362 363
    move: (Hv i) Hv'. by destruct i, lf; rewrite /= ?right_id.
  Qed.
  Lemma list_singleton_updateP' (P : A  Prop) x :
    x ~~>: P  [x] ~~>: λ k,  y, k = [y]  P y.
  Proof. eauto using list_singleton_updateP. Qed.
  Lemma list_singleton_update x y : x ~~> y  [x] ~~> [y].
  Proof.
    rewrite !cmra_update_updateP; eauto using list_singleton_updateP with subst.
Robbert Krebbers's avatar
Robbert Krebbers committed
364 365
  Qed.

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  Lemma app_updateP (P1 P2 Q : list A  Prop) l1 l2 :
    l1 ~~>: P1  l2 ~~>: P2 
    ( k1 k2, P1 k1  P2 k2  length l1 = length k1  Q (k1 ++ k2)) 
    l1 ++ l2 ~~>: Q.
  Proof.
    rewrite !cmra_total_updateP=> Hup1 Hup2 HQ n lf.
    rewrite list_op_app app_validN=> -[??].
    destruct (Hup1 n (take (length l1) lf)) as (k1&?&?); auto.
    destruct (Hup2 n (drop (length l1) lf)) as (k2&?&?); auto.
    exists (k1 ++ k2). rewrite list_op_app app_validN.
    by destruct (HQ k1 k2) as [<- ?].
  Qed.
  Lemma app_update l1 l2 k1 k2 :
    length l1 = length k1 
    l1 ~~> k1  l2 ~~> k2  l1 ++ l2 ~~> k1 ++ k2.
  Proof. rewrite !cmra_update_updateP; eauto using app_updateP with subst. Qed.

  Lemma cons_updateP (P1 : A  Prop) (P2 Q : list A  Prop) x l :
    x ~~>: P1  l ~~>: P2  ( y k, P1 y  P2 k  Q (y :: k))  x :: l ~~>: Q.
  Proof.
    intros. eapply (app_updateP _ _ _ [x]);
      naive_solver eauto using list_singleton_updateP'.
  Qed.
  Lemma cons_updateP' (P1 : A  Prop) (P2 : list A  Prop) x l :
    x ~~>: P1  l ~~>: P2  x :: l ~~>: λ k,  y k', k = y :: k'  P1 y  P2 k'.
  Proof. eauto 10 using cons_updateP. Qed.
  Lemma cons_update x y l k : x ~~> y  l ~~> k  x :: l ~~> y :: k.
  Proof. rewrite !cmra_update_updateP; eauto using cons_updateP with subst. Qed.

  Lemma list_middle_updateP (P : A  Prop) (Q : list A  Prop) l1 x l2 :
    x ~~>: P  ( y, P y  Q (l1 ++ y :: l2))  l1 ++ x :: l2 ~~>: Q.
  Proof.
    intros. eapply app_updateP.
    - by apply cmra_update_updateP.
    - by eapply cons_updateP', cmra_update_updateP.
    - naive_solver.
  Qed.
403
  Lemma list_middle_update l1 l2 x y : x ~~> y  l1 ++ x :: l2 ~~> l1 ++ y :: l2.
Robbert Krebbers's avatar
Robbert Krebbers committed
404
  Proof.
405
    rewrite !cmra_update_updateP=> ?; eauto using list_middle_updateP with subst.
Robbert Krebbers's avatar
Robbert Krebbers committed
406 407
  Qed.

408
(* FIXME
409 410 411
  Lemma list_middle_local_update l1 l2 x y ml :
    x ~l~> y @ ml = (!! length l1) 
    l1 ++ x :: l2 ~l~> l1 ++ y :: l2 @ ml.
Robbert Krebbers's avatar
Robbert Krebbers committed
412
  Proof.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    intros [Hxy Hxy']; split.
    - intros n; rewrite !list_lookup_validN=> Hl i; move: (Hl i).
      destruct (lt_eq_lt_dec i (length l1)) as [[?|?]|?]; subst.
      + by rewrite !list_lookup_opM !lookup_app_l.
      + rewrite !list_lookup_opM !list_lookup_middle // !Some_op_opM; apply (Hxy n).
      + rewrite !(cons_middle _ l1 l2) !assoc.
        rewrite !list_lookup_opM !lookup_app_r !app_length //=; lia.
    - intros n mk; rewrite !list_lookup_validN !list_dist_lookup => Hl Hl' i.
      move: (Hl i) (Hl' i).
      destruct (lt_eq_lt_dec i (length l1)) as [[?|?]|?]; subst.
      + by rewrite !list_lookup_opM !lookup_app_l.
      + rewrite !list_lookup_opM !list_lookup_middle // !Some_op_opM !inj_iff.
        apply (Hxy' n).
      + rewrite !(cons_middle _ l1 l2) !assoc.
        rewrite !list_lookup_opM !lookup_app_r !app_length //=; lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
428
  Qed.
429
  Lemma list_singleton_local_update i x y ml :
430
    x ~l~> y @ ml = (!! i)  {[ i := x ]} ~l~> {[ i := y ]} @ ml.
431
  Proof. intros; apply list_middle_local_update. by rewrite replicate_length. Qed.
432
*)
Robbert Krebbers's avatar
Robbert Krebbers committed
433 434 435
End properties.

(** Functor *)
436 437
Instance list_fmap_cmra_morphism {A B : ucmraT} (f : A  B)
  `{!CMRAMorphism f} : CMRAMorphism (fmap f : list A  list B).
Robbert Krebbers's avatar
Robbert Krebbers committed
438 439 440
Proof.
  split; try apply _.
  - intros n l. rewrite !list_lookup_validN=> Hl i. rewrite list_lookup_fmap.
441 442 443 444 445
    by apply (cmra_morphism_validN (fmap f : option A  option B)).
  - intros l. apply Some_proper. rewrite -!list_fmap_compose.
    apply list_fmap_equiv_ext, cmra_morphism_core, _.
  - intros l1 l2. apply list_equiv_lookup=>i.
    by rewrite list_lookup_op !list_lookup_fmap list_lookup_op cmra_morphism_op.
Robbert Krebbers's avatar
Robbert Krebbers committed
446 447
Qed.

448 449 450
Program Definition listURF (F : urFunctor) : urFunctor := {|
  urFunctor_car A B := listUR (urFunctor_car F A B);
  urFunctor_map A1 A2 B1 B2 fg := listC_map (urFunctor_map F fg)
Robbert Krebbers's avatar
Robbert Krebbers committed
451 452
|}.
Next Obligation.
453
  by intros F ???? n f g Hfg; apply listC_map_ne, urFunctor_ne.
Robbert Krebbers's avatar
Robbert Krebbers committed
454 455 456
Qed.
Next Obligation.
  intros F A B x. rewrite /= -{2}(list_fmap_id x).
457
  apply list_fmap_equiv_ext=>y. apply urFunctor_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
458 459 460
Qed.
Next Obligation.
  intros F A1 A2 A3 B1 B2 B3 f g f' g' x. rewrite /= -list_fmap_compose.
461
  apply list_fmap_equiv_ext=>y; apply urFunctor_compose.
Robbert Krebbers's avatar
Robbert Krebbers committed
462 463
Qed.

464 465
Instance listURF_contractive F :
  urFunctorContractive F  urFunctorContractive (listURF F).
Robbert Krebbers's avatar
Robbert Krebbers committed
466
Proof.
467
  by intros ? A1 A2 B1 B2 n f g Hfg; apply listC_map_ne, urFunctor_contractive.
Robbert Krebbers's avatar
Robbert Krebbers committed
468
Qed.
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

(** * Persistence and timelessness of lists of uPreds *)
Class PersistentL {M} (Ps : list (uPred M)) :=
  persistentL : Forall PersistentP Ps.
Arguments persistentL {_} _ {_}.
Hint Mode PersistentL + ! : typeclass_instances.

Class TimelessL {M} (Ps : list (uPred M)) :=
  timelessL : Forall TimelessP Ps.
Arguments timelessL {_} _ {_}.
Hint Mode TimelessP + ! : typeclass_instances.

Section persistent_timeless.
  Context {M : ucmraT}.
  Implicit Types Ps Qs : list (uPred M).
  Implicit Types A : Type.

  Global Instance nil_persistentL : PersistentL (@nil (uPred M)).
  Proof. constructor. Qed.
  Global Instance cons_persistentL P Ps :
    PersistentP P  PersistentL Ps  PersistentL (P :: Ps).
  Proof. by constructor. Qed.
  Global Instance app_persistentL Ps Ps' :
    PersistentL Ps  PersistentL Ps'  PersistentL (Ps ++ Ps').
  Proof. apply Forall_app_2. Qed.

  Global Instance fmap_persistentL {A} (f : A  uPred M) xs :
    ( x, PersistentP (f x))  PersistentL (f <$> xs).
  Proof. intros. apply Forall_fmap, Forall_forall; auto. Qed.
  Global Instance zip_with_persistentL {A B} (f : A  B  uPred M) xs ys :
    ( x y, PersistentP (f x y))  PersistentL (zip_with f xs ys).
  Proof.
    unfold PersistentL=> ?; revert ys; induction xs=> -[|??]; constructor; auto.
  Qed.
  Global Instance imap_persistentL {A} (f : nat  A  uPred M) xs :
    ( i x, PersistentP (f i x))  PersistentL (imap f xs).
  Proof. revert f. induction xs; simpl; constructor; naive_solver. Qed.

  (** ** Timelessness *)
  Global Instance nil_timelessL : TimelessL (@nil (uPred M)).
  Proof. constructor. Qed.
  Global Instance cons_timelessL P Ps :
    TimelessP P  TimelessL Ps  TimelessL (P :: Ps).
  Proof. by constructor. Qed.
  Global Instance app_timelessL Ps Ps' :
    TimelessL Ps  TimelessL Ps'  TimelessL (Ps ++ Ps').
  Proof. apply Forall_app_2. Qed.

  Global Instance fmap_timelessL {A} (f : A  uPred M) xs :
    ( x, TimelessP (f x))  TimelessL (f <$> xs).
  Proof. intros. apply Forall_fmap, Forall_forall; auto. Qed.
  Global Instance zip_with_timelessL {A B} (f : A  B  uPred M) xs ys :
    ( x y, TimelessP (f x y))  TimelessL (zip_with f xs ys).
  Proof.
    unfold TimelessL=> ?; revert ys; induction xs=> -[|??]; constructor; auto.
  Qed.
  Global Instance imap_timelessL {A} (f : nat  A  uPred M) xs :
    ( i x, TimelessP (f i x))  TimelessL (imap f xs).
  Proof. revert f. induction xs; simpl; constructor; naive_solver. Qed.
End persistent_timeless.