Fmap.v 23.2 KB
Newer Older
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
1
(**
charguer's avatar
charguer committed
2

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
3
This file contains a representation of finite maps
charguer's avatar
charguer committed
4 5
that may be used for representing a store. It also
provides lemmas and tactics for reasoning about
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
6
operations on the store (read, write, union).
charguer's avatar
charguer committed
7 8 9 10 11 12 13

Author: Arthur Charguéraud.
License: MIT.

*)

Set Implicit Arguments.
charguer's avatar
charguer committed
14
From TLC Require Import LibCore.
charguer's avatar
charguer committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42


(* ********************************************************************** *)
(** * Maps (partial functions) *)

(* ---------------------------------------------------------------------- *)
(* ** Representation *)

(** Type of partial functions from A to B *)

Definition map (A B : Type) : Type :=
  A -> option B.


(* ---------------------------------------------------------------------- *)
(* ** Operations *)

(** Disjoint union of two partial functions *)

Definition map_union (A B : Type) (f1 f2 : map A B) : map A B :=
  fun (x:A) => match f1 x with
           | Some y => Some y
           | None => f2 x
           end.

(** Finite domain of a partial function *)

Definition map_finite (A B : Type) (f : map A B) :=
charguer's avatar
charguer committed
43
  exists (L : list A), forall (x:A), f x <> None -> mem x L.
charguer's avatar
charguer committed
44 45 46 47 48 49

(** Disjointness of domain of two partial functions *)

Definition map_disjoint (A B : Type) (f1 f2 : map A B) :=
  forall (x:A), f1 x = None \/ f2 x = None.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
50
(** Compatibility of two partial functions on the intersection
charguer's avatar
charguer committed
51 52 53
    of their domains *)

Definition map_agree (A B : Type) (f1 f2 : map A B) :=
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
54
  forall x v1 v2,
charguer's avatar
charguer committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
  f1 x = Some v1 ->
  f2 x = Some v2 ->
  v1 = v2.


(* ---------------------------------------------------------------------- *)
(** Properties *)

Section MapOps.
Variables (A B : Type).
Implicit Types f : map A B.

(** Symmetry of disjointness *)

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
69
Lemma map_disjoint_sym :
charguer's avatar
charguer committed
70 71 72 73 74 75 76 77
  sym (@map_disjoint A B).
Proof using.
  introv H. unfolds map_disjoint. intros z. specializes H z. intuition.
Qed.

(** Commutativity of disjoint union *)

Lemma map_union_comm : forall f1 f2,
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
78
  map_disjoint f1 f2 ->
charguer's avatar
charguer committed
79
  map_union f1 f2 = map_union f2 f1.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
80
Proof using.
charguer's avatar
charguer committed
81 82
  introv H. unfold map.
  extens. intros x. unfolds map_disjoint, map_union.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
83
  specializes H x. cases (f1 x); cases (f2 x); auto. destruct H; false.
charguer's avatar
charguer committed
84 85 86 87 88
Qed.

(** Finiteness of union *)

Lemma map_union_finite : forall f1 f2,
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
89 90
  map_finite f1 ->
  map_finite f2 ->
charguer's avatar
charguer committed
91 92 93
  map_finite (map_union f1 f2).
Proof using.
  introv [L1 F1] [L2 F2]. exists (L1 ++ L2). introv M.
charguer's avatar
charguer committed
94 95
  specializes F1 x. specializes F2 x. unfold map_union in M.
  apply mem_app. destruct~ (f1 x).
charguer's avatar
charguer committed
96 97 98 99 100 101 102 103 104
Qed.

End MapOps.


(* ********************************************************************** *)
(** * Finite maps *)

(* ---------------------------------------------------------------------- *)
charguer's avatar
charguer committed
105
(** Definition of the type of finite maps *)
charguer's avatar
charguer committed
106 107 108 109 110

Inductive fmap (A B : Type) : Type := fmap_make {
  fmap_data :> map A B;
  fmap_finite : map_finite fmap_data }.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
111
Arguments fmap_make [A] [B].
charguer's avatar
charguer committed
112 113 114 115 116 117 118 119 120 121 122


(* ---------------------------------------------------------------------- *)
(** Operations *)

(** Empty fmap *)

Program Definition fmap_empty (A B : Type) : fmap A B :=
  fmap_make (fun l => None) _.
Next Obligation. exists (@nil A). auto_false. Qed.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
123
Arguments fmap_empty {A} {B}.
charguer's avatar
charguer committed
124 125 126

(** Singleton fmap *)

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
127
Program Definition fmap_single A B (x:A) (v:B) : fmap A B :=
charguer's avatar
charguer committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  fmap_make (fun x' => If x = x' then Some v else None) _.
Next Obligation.
  exists (x::nil). intros. case_if. subst~.
Qed.

(** Union of fmaps *)

Program Definition fmap_union A B (h1 h2:fmap A B) : fmap A B :=
  fmap_make (map_union h1 h2) _.
Next Obligation. destruct h1. destruct h2. apply~ map_union_finite. Qed.

Notation "h1 \+ h2" := (fmap_union h1 h2)
   (at level 51, right associativity) : fmap_scope.

Open Scope fmap_scope.

(** Update of a fmap *)

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
146 147
Definition fmap_update A B (h:fmap A B) (x:A) (v:B) :=
  fmap_union (fmap_single x v) h.
charguer's avatar
charguer committed
148 149 150 151 152 153 154 155 156
  (* Note: the union operation first reads in the first argument. *)


(* ---------------------------------------------------------------------- *)
(** Properties *)

(** Inhabited type [fmap] *)

Global Instance Inhab_fmap A B : Inhab (fmap A B).
charguer's avatar
charguer committed
157
Proof using. intros. applys Inhab_of_val (@fmap_empty A B). Qed.
charguer's avatar
charguer committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

(** Compatible fmaps *)

Definition fmap_agree A B (h1 h2:fmap A B) :=
  map_agree h1 h2.

(** Disjoint fmaps *)

Definition fmap_disjoint A B (h1 h2 : fmap A B) : Prop :=
  map_disjoint h1 h2.

Notation "\# h1 h2" := (fmap_disjoint h1 h2)
  (at level 40, h1 at level 0, h2 at level 0, no associativity) : fmap_scope.

(** Three disjoint fmaps *)

Definition fmap_disjoint_3 A B (h1 h2 h3 : fmap A B) :=
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
175 176
     fmap_disjoint h1 h2
  /\ fmap_disjoint h2 h3
charguer's avatar
charguer committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  /\ fmap_disjoint h1 h3.

Notation "\# h1 h2 h3" := (fmap_disjoint_3 h1 h2 h3)
  (at level 40, h1 at level 0, h2 at level 0, h3 at level 0, no associativity)
  : fmap_scope.



(* ********************************************************************** *)
(* * Lemmas about Fmap *)

Section FmapProp.
Variables (A B : Type).
Implicit Types f g h : fmap A B.

(* ---------------------------------------------------------------------- *)
(* ** Equality *)

charguer's avatar
charguer committed
195
Lemma fmap_make_eq : forall (f1 f2:map A B) F1 F2,
charguer's avatar
charguer committed
196 197 198 199 200 201 202
  (forall x, f1 x = f2 x) ->
  fmap_make f1 F1 = fmap_make f2 F2.
Proof using.
  introv H. asserts: (f1 = f2). { unfold map. extens~. }
  subst. fequals. (* note: involves proof irrelevance *)
Qed.

charguer's avatar
charguer committed
203 204 205 206 207
Lemma fmap_eq_inv_fmap_data_eq : forall h1 h2,
  h1 = h2 ->
  forall x, fmap_data h1 x = fmap_data h2 x.
Proof using. intros. fequals. Qed.

charguer's avatar
charguer committed
208 209 210 211 212

(* ---------------------------------------------------------------------- *)
(* ** Disjointness *)

Lemma fmap_disjoint_sym : forall h1 h2,
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
213
  \# h1 h2 ->
charguer's avatar
charguer committed
214
  \# h2 h1.
charguer's avatar
charguer committed
215 216 217 218
Proof using. intros [f1 F1] [f2 F2]. apply map_disjoint_sym. Qed.

Lemma fmap_disjoint_comm : forall h1 h2,
  \# h1 h2 = \# h2 h1.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
219
Proof using. lets: fmap_disjoint_sym. extens*. Qed.
charguer's avatar
charguer committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

Lemma fmap_disjoint_empty_l : forall h,
  \# fmap_empty h.
Proof using. intros [f F] x. simple~. Qed.

Lemma fmap_disjoint_empty_r : forall h,
  \# h fmap_empty.
Proof using. intros [f F] x. simple~. Qed.

Hint Resolve fmap_disjoint_sym fmap_disjoint_empty_l fmap_disjoint_empty_r.

Lemma fmap_disjoint_union_eq_r : forall h1 h2 h3,
  \# h1 (h2 \+ h3) =
  (\# h1 h2 /\ \# h1 h3).
Proof using.
  intros [f1 F1] [f2 F2] [f3 F3].
  unfolds fmap_disjoint, fmap_union. simpls.
  unfolds map_disjoint, map_union. extens. iff M [M1 M2].
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
238
  split; intros x; specializes M x;
charguer's avatar
charguer committed
239
   destruct (f2 x); intuition; tryfalse.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
240
  intros x. specializes M1 x. specializes M2 x.
charguer's avatar
charguer committed
241 242 243 244 245 246 247
   destruct (f2 x); intuition.
Qed.

Lemma fmap_disjoint_union_eq_l : forall h1 h2 h3,
  \# (h2 \+ h3) h1 =
  (\# h1 h2 /\ \# h1 h3).
Proof using.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
248
  intros. rewrite fmap_disjoint_comm.
charguer's avatar
charguer committed
249 250 251
  apply fmap_disjoint_union_eq_r.
Qed.

charguer's avatar
alloc  
charguer committed
252 253 254 255 256 257 258
Lemma fmap_disjoint_single_single : forall (x1 x2:A) (v1 v2:B),
  x1 <> x2 ->
  \# (fmap_single x1 v1) (fmap_single x2 v2).
Proof using.
  introv N. intros x. simpls. do 2 case_if; auto.
Qed.

charguer's avatar
fun2  
charguer committed
259
Lemma fmap_disjoint_single_single_same_inv : forall (x:A) (v1 v2:B),
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
260
  \# (fmap_single x v1) (fmap_single x v2) ->
charguer's avatar
charguer committed
261
  False.
charguer's avatar
charguer committed
262 263 264 265 266 267 268 269 270 271 272 273
Proof using.
  introv D. specializes D x. simpls. case_if. destruct D; tryfalse.
Qed.

Lemma fmap_disjoint_3_unfold : forall h1 h2 h3,
  \# h1 h2 h3 = (\# h1 h2 /\ \# h2 h3 /\ \# h1 h3).
Proof using. auto. Qed.

Lemma fmap_disjoint_single_set : forall h l v1 v2,
  \# (fmap_single l v1) h ->
  \# (fmap_single l v2) h.
Proof using.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
274
  introv M. unfolds fmap_disjoint, fmap_single, map_disjoint; simpls.
charguer's avatar
charguer committed
275 276 277 278 279 280 281
  intros l'. specializes M l'. case_if~. destruct M; auto_false.
Qed.


(* ---------------------------------------------------------------------- *)
(* ** Union *)

charguer's avatar
charguer committed
282
Lemma fmap_union_self : forall h,
charguer's avatar
charguer committed
283 284
  h \+ h = h.
Proof using.
charguer's avatar
charguer committed
285
  intros [f F]. apply~ fmap_make_eq. simpl. intros x.
charguer's avatar
charguer committed
286 287 288 289 290
  unfold map_union. cases~ (f x).
Qed.

Lemma fmap_union_empty_l : forall h,
  fmap_empty \+ h = h.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
291
Proof using.
charguer's avatar
charguer committed
292
  intros [f F]. unfold fmap_union, map_union, fmap_empty. simpl.
charguer's avatar
charguer committed
293
  apply~ fmap_make_eq.
charguer's avatar
charguer committed
294 295 296 297
Qed.

Lemma fmap_union_empty_r : forall h,
  h \+ fmap_empty = h.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
298
Proof using.
charguer's avatar
charguer committed
299
  intros [f F]. unfold fmap_union, map_union, fmap_empty. simpl.
charguer's avatar
charguer committed
300
  apply fmap_make_eq. intros x. destruct~ (f x).
charguer's avatar
charguer committed
301 302 303 304 305 306 307
Qed.

Lemma fmap_union_eq_empty_inv_l : forall h1 h2,
  h1 \+ h2 = fmap_empty ->
  h1 = fmap_empty.
Proof using.
  intros (f1&F1) (f2&F2) M. inverts M as M.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
308
  applys fmap_make_eq. intros l.
charguer's avatar
charguer committed
309 310
  unfolds map_union.
  lets: fun_eq_1 l M.
charguer's avatar
charguer committed
311 312 313 314 315 316 317 318
  cases (f1 l); auto_false.
Qed.

Lemma fmap_union_eq_empty_inv_r : forall h1 h2,
  h1 \+ h2 = fmap_empty ->
  h2 = fmap_empty.
Proof using.
  intros (f1&F1) (f2&F2) M. inverts M as M.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
319 320
  applys fmap_make_eq. intros l.
  unfolds map_union.
charguer's avatar
charguer committed
321
  lets: fun_eq_1 l M.
charguer's avatar
charguer committed
322 323 324
  cases (f1 l); auto_false.
Qed.

charguer's avatar
charguer committed
325
Lemma fmap_union_comm_of_disjoint : forall h1 h2,
charguer's avatar
charguer committed
326 327 328
  \# h1 h2 ->
  h1 \+ h2 = h2 \+ h1.
Proof using.
charguer's avatar
charguer committed
329
  intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl.
charguer's avatar
charguer committed
330 331 332
  intros. rewrite~ map_union_comm.
Qed.

charguer's avatar
charguer committed
333
Lemma fmap_union_comm_of_agree : forall h1 h2,
charguer's avatar
charguer committed
334 335 336
  fmap_agree h1 h2 ->
  h1 \+ h2 = h2 \+ h1.
Proof using.
charguer's avatar
charguer committed
337
  intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl.
charguer's avatar
charguer committed
338 339 340 341 342 343 344 345
  intros l. specializes H l. unfolds map_union. simpls.
  cases (f1 l); cases (f2 l); auto. fequals. applys* H.
Qed.

Lemma fmap_union_assoc : forall h1 h2 h3,
  (h1 \+ h2) \+ h3 = h1 \+ (h2 \+ h3).
Proof using.
  intros [f1 F1] [f2 F2] [f3 F3]. unfolds fmap_union. simpls.
charguer's avatar
charguer committed
346
  apply fmap_make_eq. intros x. unfold map_union. destruct~ (f1 x).
charguer's avatar
charguer committed
347 348 349
Qed.

(*
charguer's avatar
charguer committed
350
Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1' : fmap,
charguer's avatar
charguer committed
351 352 353 354 355 356
  \# h1 h2 ->
  fmap_agree h1' h2 ->
  h1 \+ h2 = h1' \+ h2 ->
  h1 = h1'.
Proof using.
  intros [f2 F2] [f1 F1] [f1' F1'] D D' E.
charguer's avatar
charguer committed
357 358
  applys fmap_make_eq. intros x. specializes D x. specializes D' x.
  lets E': fmap_eq_inv_fmap_data_eq (rm E) x. simpls.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
359
  unfolds map_union.
charguer's avatar
charguer committed
360 361 362 363 364 365 366 367 368 369
  cases (f1 x); cases (f2 x); try solve [cases (f1' x); destruct D; congruence ].
  destruct D; try false.
  rewrite H in E'. inverts E'.
  cases (f1' x); cases (f1 x);
    destruct D; try congruence.
  false.
    destruct D'; try congruence.
Qed.
*)

charguer's avatar
charguer committed
370
Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1',
charguer's avatar
charguer committed
371 372 373 374 375 376
  \# h1 h2 ->
  \# h1' h2 ->
  h1 \+ h2 = h1' \+ h2 ->
  h1 = h1'.
Proof using.
  intros [f2 F2] [f1' F1'] [f1 F1] D D' E.
charguer's avatar
charguer committed
377 378
  applys fmap_make_eq. intros x. specializes D x. specializes D' x.
  lets E': fmap_eq_inv_fmap_data_eq (rm E) x. simpls.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
379
  unfolds map_union.
charguer's avatar
charguer committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  cases (f1' x); cases (f1 x);
    destruct D; try congruence;
    destruct D'; try congruence.
Qed.


(* ---------------------------------------------------------------------- *)
(* ** Compatibility *)

Lemma fmap_agree_refl : forall h,
  fmap_agree h h.
Proof using.
  intros h. introv M1 M2. congruence.
Qed.

Lemma fmap_agree_sym : forall f1 f2,
  fmap_agree f1 f2 ->
  fmap_agree f2 f1.
Proof using.
  introv M. intros l v1 v2 E1 E2.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
400
  specializes M l E1.
charguer's avatar
charguer committed
401 402
Qed.

charguer's avatar
charguer committed
403
Lemma fmap_agree_of_disjoint : forall h1 h2,
charguer's avatar
charguer committed
404 405
  fmap_disjoint h1 h2 ->
  fmap_agree h1 h2.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
406
Proof using.
charguer's avatar
charguer committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  introv HD. intros l v1 v2 M1 M2. destruct (HD l); false.
Qed.

Lemma fmap_agree_empty_l : forall h,
  fmap_agree fmap_empty h.
Proof using. intros h l v1 v2 E1 E2. simpls. false. Qed.

Lemma fmap_agree_empty_r : forall h,
  fmap_agree h fmap_empty.
Proof using.
  hint fmap_agree_sym, fmap_agree_empty_l. eauto.
Qed.

Lemma fmap_agree_union_l : forall f1 f2 f3,
  fmap_agree f1 f3 ->
  fmap_agree f2 f3 ->
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
423
  fmap_agree (f1 \+ f2) f3.
charguer's avatar
charguer committed
424 425 426 427 428 429 430 431 432 433 434
Proof using.
  introv M1 M2. intros l v1 v2 E1 E2.
  specializes M1 l. specializes M2 l.
  simpls. unfolds map_union. cases (fmap_data f1 l).
  { inverts E1. applys* M1. }
  { applys* M2. }
Qed.

Lemma fmap_agree_union_r : forall f1 f2 f3,
  fmap_agree f1 f2 ->
  fmap_agree f1 f3 ->
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
435
  fmap_agree f1 (f2 \+ f3).
charguer's avatar
charguer committed
436 437 438 439 440 441 442 443 444 445 446 447
Proof using.
  hint fmap_agree_sym, fmap_agree_union_l. eauto.
Qed.

Lemma fmap_agree_union_lr : forall f1 g1 f2 g2,
  fmap_agree g1 g2 ->
  \# f1 f2 (g1 \+ g2) ->
  fmap_agree (f1 \+ g1) (f2 \+ g2).
Proof using.
  introv M1 (M2a&M2b&M2c).
  rewrite fmap_disjoint_union_eq_r in *.
  applys fmap_agree_union_l; applys fmap_agree_union_r;
charguer's avatar
charguer committed
448
    try solve [ applys* fmap_agree_of_disjoint ].
charguer's avatar
charguer committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  auto.
Qed.

Lemma fmap_agree_union_ll_inv : forall f1 f2 f3,
  fmap_agree (f1 \+ f2) f3 ->
  fmap_agree f1 f3.
Proof using.
  introv M. intros l v1 v2 E1 E2.
  specializes M l. simpls. unfolds map_union.
  rewrite E1 in M. applys* M.
Qed.

Lemma fmap_agree_union_rl_inv : forall f1 f2 f3,
  fmap_agree f1 (f2 \+ f3) ->
  fmap_agree f1 f2.
Proof using.
  hint fmap_agree_union_ll_inv, fmap_agree_sym. eauto.
Qed.

Lemma fmap_agree_union_lr_inv_agree_agree : forall f1 f2 f3,
  fmap_agree (f1 \+ f2) f3 ->
  fmap_agree f1 f2 ->
  fmap_agree f2 f3.
Proof using.
charguer's avatar
charguer committed
473
  introv M D. rewrite~ (@fmap_union_comm_of_agree f1 f2) in M.
charguer's avatar
charguer committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487
  applys* fmap_agree_union_ll_inv.
Qed.

Lemma fmap_agree_union_rr_inv_agree : forall f1 f2 f3,
  fmap_agree f1 (f2 \+ f3) ->
  fmap_agree f2 f3 ->
  fmap_agree f1 f3.
Proof using.
  hint fmap_agree_union_lr_inv_agree_agree, fmap_agree_sym. eauto.
Qed.

Lemma fmap_agree_union_l_inv : forall f1 f2 f3,
  fmap_agree (f1 \+ f2) f3 ->
  fmap_agree f1 f2 ->
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
488
     fmap_agree f1 f3
charguer's avatar
charguer committed
489 490
  /\ fmap_agree f2 f3.
Proof using.
charguer's avatar
charguer committed
491
  (* LATER: proofs redundant with others above *)
charguer's avatar
charguer committed
492
  introv M2 M1. split.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
493
  { intros l v1 v2 E1 E2.
charguer's avatar
charguer committed
494 495 496
    specializes M1 l v1 v2 E1. applys~ M2 l v1 v2.
    unfold fmap_union, map_union; simpl. rewrite~ E1. }
  { intros l v1 v2 E1 E2.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
497
    specializes M1 l. specializes M2 l.
charguer's avatar
charguer committed
498 499
    unfolds fmap_union, map_union; simpls.
    cases (fmap_data f1 l). (* LATER: name b *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
500
    { applys eq_trans b. symmetry. applys~ M1. applys~ M2. }
charguer's avatar
charguer committed
501 502 503 504 505 506
    { auto. } }
Qed.

Lemma fmap_agree_union_r_inv : forall f1 f2 f3,
  fmap_agree f1 (f2 \+ f3) ->
  fmap_agree f2 f3 ->
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
507
     fmap_agree f1 f2
charguer's avatar
charguer committed
508
  /\ fmap_agree f1 f3.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
509
Proof using.
charguer's avatar
charguer committed
510 511 512 513 514 515 516 517
  hint fmap_agree_sym.
  intros. forwards~ (M1&M2): fmap_agree_union_l_inv f2 f3 f1.
Qed.


(* ---------------------------------------------------------------------- *)
(* ** Read and write *)

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
518
Lemma fmap_union_single_l_read : forall f1 f2 l v,
charguer's avatar
charguer committed
519 520 521 522 523 524
  f1 = fmap_single l v ->
  fmap_data (f1 \+ f2) l = Some v.
Proof using.
  intros. subst. simpl. unfold map_union. case_if~.
Qed.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
525
Lemma fmap_union_single_to_update : forall f1 f1' f2 l v v',
charguer's avatar
charguer committed
526 527 528 529 530
  f1 = fmap_single l v ->
  f1' = fmap_single l v' ->
  (f1' \+ f2) = fmap_update (f1 \+ f2) l v'.
Proof using.
  intros. subst. unfold fmap_update.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
531
  rewrite <- fmap_union_assoc. fequals.
charguer's avatar
charguer committed
532
  applys fmap_make_eq. intros l'.
charguer's avatar
charguer committed
533 534 535 536 537
  unfolds map_union, fmap_single; simpl. case_if~.
Qed.

End FmapProp.

charguer's avatar
charguer committed
538 539 540
Arguments fmap_union_assoc [A] [B].
Arguments fmap_union_comm_of_disjoint [A] [B].
Arguments fmap_union_comm_of_agree [A] [B].
charguer's avatar
charguer committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555


(* ********************************************************************** *)
(* * Tactics *)

(* ---------------------------------------------------------------------- *)
(* ** Tactic [fmap_disjoint] for proving disjointness results *)

(** [fmap_disjoint] proves goals of the form [\# h1 h2] and
    [\# h1 h2 h3] by expanding all hypotheses into binary forms
    [\# h1 h2] and then exploiting symmetry and disjointness
    with [fmap_empty]. *)

Hint Resolve fmap_disjoint_sym fmap_disjoint_empty_l fmap_disjoint_empty_r.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
556
Hint Rewrite
charguer's avatar
charguer committed
557 558 559 560 561 562 563 564 565 566 567 568 569
  fmap_disjoint_union_eq_l
  fmap_disjoint_union_eq_r
  fmap_disjoint_3_unfold : rew_disjoint.

Tactic Notation "rew_disjoint" :=
  autorewrite with rew_disjoint in *.
Tactic Notation "rew_disjoint" "*" :=
  rew_disjoint; auto_star.

Tactic Notation "fmap_disjoint" :=
  solve [ subst; rew_disjoint; jauto_set; auto ].

Tactic Notation "fmap_disjoint_if_needed" :=
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
570
  match goal with
charguer's avatar
charguer committed
571 572 573 574 575 576 577 578 579 580 581 582 583
  | |- \# _ _ => fmap_disjoint
  | |- \# _ _ _ => fmap_disjoint
  end.

Lemma fmap_disjoint_demo : forall A B (h1 h2 h3 h4 h5:fmap A B),
  h1 = h2 \+ h3 ->
  \# h2 h3 ->
  \# h1 h4 h5 ->
  \# h3 h2 h5 /\ \# h4 h5.
Proof using.
  intros. dup 2.
  { subst. rew_disjoint. jauto_set. auto. auto. auto. auto. }
  { fmap_disjoint. }
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
584
Qed.
charguer's avatar
charguer committed
585 586 587 588 589 590 591 592 593 594


(* ---------------------------------------------------------------------- *)
(* ** Tactic [fmap_eq] for proving equality of fmaps,
      and tactic [rew_fmap] to normalize fmap expressions. *)

Section StateEq.
Variables (A B : Type).
Implicit Types h : fmap A B.

charguer's avatar
charguer committed
595 596
(** [fmap_eq] proves equalities between unions of fmaps, of the form
    [h1 \+ h2 \+ h3 \+ h4 = h1' \+ h2' \+ h3' \+ h4']
charguer's avatar
charguer committed
597
    It attempts to discharge the disjointness side-conditions.
charguer's avatar
charguer committed
598
    Disclaimer: it cancels heaps at depth up to 4, but no more. *)
charguer's avatar
charguer committed
599 600 601 602

Lemma fmap_union_eq_cancel_1 : forall h1 h2 h2',
  h2 = h2' ->
  h1 \+ h2 = h1 \+ h2'.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
603
Proof using. intros. subst. auto. Qed.
charguer's avatar
charguer committed
604 605 606

Lemma fmap_union_eq_cancel_1' : forall h1,
  h1 = h1.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
607
Proof using. intros. auto. Qed.
charguer's avatar
charguer committed
608 609 610 611 612 613 614

Lemma fmap_union_eq_cancel_2 : forall h1 h1' h2 h2',
  \# h1 h1' ->
  h2 = h1' \+ h2' ->
  h1 \+ h2 = h1' \+ h1 \+ h2'.
Proof using.
  intros. subst. rewrite <- fmap_union_assoc.
charguer's avatar
charguer committed
615
  rewrite (fmap_union_comm_of_disjoint h1 h1').
charguer's avatar
charguer committed
616
  rewrite~ fmap_union_assoc. auto.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
617
Qed.
charguer's avatar
charguer committed
618 619 620 621 622 623

Lemma fmap_union_eq_cancel_2' : forall h1 h1' h2,
  \# h1 h1' ->
  h2 = h1' ->
  h1 \+ h2 = h1' \+ h1.
Proof using.
charguer's avatar
charguer committed
624
  intros. subst. apply~ fmap_union_comm_of_disjoint.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
625
Qed.
charguer's avatar
charguer committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

Lemma fmap_union_eq_cancel_3 : forall h1 h1' h2 h2' h3',
  \# h1 (h1' \+ h2') ->
  h2 = h1' \+ h2' \+ h3' ->
  h1 \+ h2 = h1' \+ h2' \+ h1 \+ h3'.
Proof using.
  intros. subst.
  rewrite <- (fmap_union_assoc h1' h2' h3').
  rewrite <- (fmap_union_assoc h1' h2' (h1 \+ h3')).
  apply~ fmap_union_eq_cancel_2.
Qed.

Lemma fmap_union_eq_cancel_3' : forall h1 h1' h2 h2',
  \# h1 (h1' \+ h2') ->
  h2 = h1' \+ h2' ->
  h1 \+ h2 = h1' \+ h2' \+ h1.
Proof using.
  intros. subst.
  rewrite <- (fmap_union_assoc h1' h2' h1).
  apply~ fmap_union_eq_cancel_2'.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
646
Qed.
charguer's avatar
charguer committed
647 648 649 650 651 652 653 654 655 656

Lemma fmap_union_eq_cancel_4 : forall h1 h1' h2 h2' h3' h4',
  \# h1 ((h1' \+ h2') \+ h3') ->
  h2 = h1' \+ h2' \+ h3' \+ h4' ->
  h1 \+ h2 = h1' \+ h2' \+ h3' \+ h1 \+ h4'.
Proof using.
  intros. subst.
  rewrite <- (fmap_union_assoc h1' h2' (h3' \+ h4')).
  rewrite <- (fmap_union_assoc h1' h2' (h3' \+ h1 \+ h4')).
  apply~ fmap_union_eq_cancel_3.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
657
Qed.
charguer's avatar
charguer committed
658 659 660 661 662 663 664 665 666

Lemma fmap_union_eq_cancel_4' : forall h1 h1' h2 h2' h3',
  \# h1 (h1' \+ h2' \+ h3') ->
  h2 = h1' \+ h2' \+ h3' ->
  h1 \+ h2 = h1' \+ h2' \+ h3' \+ h1.
Proof using.
  intros. subst.
  rewrite <- (fmap_union_assoc h2' h3' h1).
  apply~ fmap_union_eq_cancel_3'.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
667
Qed.
charguer's avatar
charguer committed
668 669 670

End StateEq.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
671
Hint Rewrite
charguer's avatar
charguer committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  fmap_union_assoc
  fmap_union_empty_l
  fmap_union_empty_r : rew_fmap.

Tactic Notation "rew_fmap" :=
  autorewrite with rew_fmap in *.

Tactic Notation "rew_fmap" "~" :=
  rew_fmap; auto_tilde.

Tactic Notation "rew_fmap" "*" :=
  rew_fmap; auto_star.

Ltac fmap_eq_step tt :=
  match goal with | |- ?G => match G with
  | ?h1 = ?h1 => apply fmap_union_eq_cancel_1'
  | ?h1 \+ ?h2 = ?h1 \+ ?h2' => apply fmap_union_eq_cancel_1
  | ?h1 \+ ?h2 = ?h1' \+ ?h1 => apply fmap_union_eq_cancel_2'
  | ?h1 \+ ?h2 = ?h1' \+ ?h1 \+ ?h2' => apply fmap_union_eq_cancel_2
  | ?h1 \+ ?h2 = ?h1' \+ ?h2' \+ ?h1 => apply fmap_union_eq_cancel_3'
  | ?h1 \+ ?h2 = ?h1' \+ ?h2' \+ ?h1 \+ ?h3' => apply fmap_union_eq_cancel_3
  | ?h1 \+ ?h2 = ?h1' \+ ?h2' \+ ?h3' \+ ?h1 => apply fmap_union_eq_cancel_4'
  | ?h1 \+ ?h2 = ?h1' \+ ?h2' \+ ?h3' \+ ?h1 \+ ?h4' => apply fmap_union_eq_cancel_4
  end end.

Tactic Notation "fmap_eq" :=
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
698 699
  subst;
  rew_fmap;
charguer's avatar
charguer committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
  repeat (fmap_eq_step tt);
  try fmap_disjoint_if_needed.

Tactic Notation "fmap_eq" "~" :=
  fmap_eq; auto_tilde.

Tactic Notation "fmap_eq" "*" :=
  fmap_eq; auto_star.

Lemma fmap_eq_demo : forall A B (h1 h2 h3 h4 h5:fmap A B),
  \# h1 h2 h3 ->
  \# (h1 \+ h2 \+ h3) h4 h5 ->
  h1 = h2 \+ h3 ->
  h4 \+ h1 \+ h5 = h2 \+ h5 \+ h4 \+ h3.
Proof using.
  intros. dup 2.
  { subst. rew_fmap.
    fmap_eq_step tt. fmap_disjoint.
    fmap_eq_step tt.
    fmap_eq_step tt. fmap_disjoint. auto. }
  { fmap_eq. }
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
721
Qed.
charguer's avatar
charguer committed
722 723 724


(* ---------------------------------------------------------------------- *)
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
725
(* ** Tactic [fmap_red] for proving [red] goals
charguer's avatar
charguer committed
726 727 728 729 730 731
      (reduction according to a big-step semantics)
      modulo equalities between fmaps *)

(** [fmap_red] proves a goal of the form [red h1 t h2 v]
    using an hypothesis of the shape [red h1' t h2' v],
    generating [h1 = h1'] and [h2 = h2'] as subgoals, and
charguer's avatar
charguer committed
732 733 734
    attempting to solve them using the tactic [fmap_eq].
    The tactic should be configured depending on [red].
    For example:
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
735

charguer's avatar
charguer committed
736 737 738
       Ltac fmap_red_base tt :=
        match goal with H: red _ ?t _ _ |- red _ ?t _ _ =>
          applys_eq H 2 4; try fmap_eq end.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
739

charguer's avatar
charguer committed
740 741
    The default implementation is a dummy one.
*)
charguer's avatar
charguer committed
742 743 744

Ltac fmap_red_base tt := fail.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
745
Tactic Notation "fmap_red" :=
charguer's avatar
charguer committed
746 747
  fmap_red_base tt.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
748
Tactic Notation "fmap_red" "~" :=
charguer's avatar
charguer committed
749 750
  fmap_red; auto_tilde.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
751
Tactic Notation "fmap_red" "*" :=
charguer's avatar
charguer committed
752
  fmap_red; auto_star.
charguer's avatar
alloc  
charguer committed
753 754 755 756 757 758 759 760


(* ********************************************************************** *)
(** * Consecutive locations and fresh locations *)

(* ---------------------------------------------------------------------- *)
(** ** Existence of fresh locations *)

charguer's avatar
array  
charguer committed
761
Fixpoint fmap_conseq (B:Type) (l:nat) (k:nat) (v:B) : fmap nat B :=
charguer's avatar
alloc  
charguer committed
762
  match k with
charguer's avatar
array  
charguer committed
763 764
  | O => fmap_empty
  | S k' => (fmap_single l v) \+ (fmap_conseq (S l) k' v)
charguer's avatar
alloc  
charguer committed
765 766
  end.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
767
Lemma fmap_conseq_zero : forall B (l:nat) (v:B),
charguer's avatar
alloc  
charguer committed
768 769 770
  fmap_conseq l O v = fmap_empty.
Proof using. auto. Qed.

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
771
Lemma fmap_conseq_succ : forall B (l:nat) (k:nat) (v:B),
charguer's avatar
array  
charguer committed
772
  fmap_conseq l (S k) v = (fmap_single l v) \+ (fmap_conseq (S l) k v).
charguer's avatar
alloc  
charguer committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
Proof using. auto. Qed.

Opaque fmap_conseq.


(* ---------------------------------------------------------------------- *)
(** ** Existence of fresh locations *)

(** These lemmas are useful to prove:
    [forall h v, exists l, fmap_disjoint (fmap_single l v) h]. *)

Definition loc_fresh_gen (L : list nat) :=
  (1 + fold_right plus O L)%nat.

Lemma loc_fresh_ind : forall l L,
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
788
  mem l L ->
charguer's avatar
alloc  
charguer committed
789 790 791
  (l < loc_fresh_gen L)%nat.
Proof using.
  intros l L. unfold loc_fresh_gen.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
792
  induction L; introv M; inverts M; rew_listx.
charguer's avatar
alloc  
charguer committed
793 794 795 796 797
  { math. }
  { forwards~: IHL. math. }
Qed.

Lemma loc_fresh_nat_ge : forall (L:list nat),
charguer's avatar
charguer committed
798
  exists (l:nat), forall (i:nat), ~ mem (l+i)%nat L.
charguer's avatar
alloc  
charguer committed
799 800 801 802 803 804
Proof using.
  intros L. exists (loc_fresh_gen L). intros i M.
  lets: loc_fresh_ind M. math.
Qed.

Lemma loc_fresh_nat : forall (L:list nat),
charguer's avatar
charguer committed
805
  exists (l:nat), ~ mem l L.
charguer's avatar
alloc  
charguer committed
806
Proof using.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
807
  intros L. forwards (l&P): loc_fresh_nat_ge L.
charguer's avatar
alloc  
charguer committed
808 809 810 811 812
  exists l. intros M. applys (P 0%nat). applys_eq M 2. math.
Qed.


(* ---------------------------------------------------------------------- *)
charguer's avatar
charguer committed
813
(** ** Extension of a number of consecutive fresh locations *)
charguer's avatar
alloc  
charguer committed
814 815 816 817 818 819 820 821 822 823 824 825

Section FmapFresh.
Variables (B : Type).
Implicit Types h : fmap nat B.

Lemma fmap_single_fresh : forall null h v,
  exists l, \# (fmap_single l v) h /\ l <> null.
Proof using.
  intros null (m&(L&M)) v.
  unfold fmap_disjoint, map_disjoint. simpl.
  lets (l&F): (loc_fresh_nat (null::L)).
  exists l. split.
charguer's avatar
charguer committed
826
  { intros l'. case_if~. (* --LATER: fix TLC substitution in case_if *)
charguer's avatar
charguer committed
827
    { subst. right. applys not_not_inv. intros H. applys F.
charguer's avatar
alloc  
charguer committed
828 829 830 831 832 833 834 835 836 837 838
      constructor. applys~ M. } }
  { intro_subst. applys~ F. }
Qed.

Lemma fmap_conseq_fresh : forall null h k v,
  exists l, \# (fmap_conseq l k v) h /\ l <> null.
Proof using.
  intros null (m&(L&M)) k v.
  unfold fmap_disjoint, map_disjoint. simpl.
  lets (l&F): (loc_fresh_nat_ge (null::L)).
  exists l. split.
charguer's avatar
array  
charguer committed
839 840 841 842 843
  { intros l'. gen l. induction k; intros.
    { simple~. }
    { rewrite fmap_conseq_succ.
      destruct (IHk (S l)%nat) as [E|?].
      { intros i N. applys F (S i). applys_eq N 2. math. }
charguer's avatar
charguer committed
844
      { simpl. unfold map_union. case_if~. 
charguer's avatar
charguer committed
845
        { subst. right. applys not_not_inv. intros H. applys F 0%nat.
charguer's avatar
array  
charguer committed
846
          constructor. math_rewrite (l'+0 = l')%nat. applys~ M. } }
charguer's avatar
alloc  
charguer committed
847 848 849 850 851
      { auto. } } }
  { intro_subst. applys~ F 0%nat. rew_nat. auto. }
Qed.

Lemma fmap_disjoint_single_conseq : forall B l l' k (v:B),
charguer's avatar
array  
charguer committed
852 853
  (l < l')%nat \/ (l >= l'+k)%nat ->
  \# (fmap_single l v) (fmap_conseq l' k v).
charguer's avatar
alloc  
charguer committed
854
Proof using.
charguer's avatar
array  
charguer committed
855
  introv N. gen l'. induction k; intros.
charguer's avatar
alloc  
charguer committed
856 857
  { rewrite~ fmap_conseq_zero. }
  { rewrite fmap_conseq_succ. rew_disjoint. split.
charguer's avatar
array  
charguer committed
858 859
    { applys fmap_disjoint_single_single. destruct N; math. }
    { applys IHk. destruct N. { left; math. } { right; math. } } }
charguer's avatar
alloc  
charguer committed
860 861 862
Qed.

End FmapFresh.