Fmap.v 21.1 KB
Newer Older
charguer's avatar
charguer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 43 44
(** 

This file contains a representation of finite maps 
that may be used for representing a store. It also
provides lemmas and tactics for reasoning about
operations on the store (read, write, union). 

Author: Arthur Charguéraud.
License: MIT.

*)

Set Implicit Arguments.
Require Import LibCore.


(* ********************************************************************** *)

Tactic Notation "cases" constr(E) :=  (* TODO For TLC *)
  let H := fresh "Eq" in cases E as H.


(* ********************************************************************** *)
(** * Fresh locations *)

(* ---------------------------------------------------------------------- *)
(** ** 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 x L,
  Mem x L -> 
  (x < loc_fresh_gen L)%nat.
Proof using.
  intros x L. unfold loc_fresh_gen.
  induction L; introv M; inverts M; rew_list. 
  { math. }
  { forwards~: IHL. math. }
Qed.

charguer's avatar
charguer committed
45
Definition loc_fresh_exists (A:Type) := 
charguer's avatar
charguer committed
46 47
  forall (L:list A), exists (x:A), ~ Mem x L.

charguer's avatar
charguer committed
48
Lemma loc_fresh_nat : loc_fresh_exists nat.
charguer's avatar
charguer committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
Proof using.
  intros L. exists (loc_fresh_gen L). intros M.
  lets: loc_fresh_ind M. math.
Qed.

Hint Resolve loc_fresh_nat.


(* ********************************************************************** *)
(** * 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) :=
  exists (L : list A), forall (x:A), f x <> None -> Mem x L.

(** 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.

(** Compatibility of two partial functions on the intersection 
    of their domains *)

Definition map_agree (A B : Type) (f1 f2 : map A B) :=
  forall x v1 v2, 
  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 *)

Lemma map_disjoint_sym : 
  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,
  map_disjoint f1 f2 -> 
  map_union f1 f2 = map_union f2 f1.
Proof using. 
  introv H. unfold map.
  extens. intros x. unfolds map_disjoint, map_union.
  specializes H x. cases (f1 x); cases (f2 x); auto. destruct H; false. 
Qed.

(** Finiteness of union *)

Lemma map_union_finite : forall f1 f2,
  map_finite f1 -> 
  map_finite f2 -> 
  map_finite (map_union f1 f2).
Proof using.
  introv [L1 F1] [L2 F2]. exists (L1 ++ L2). introv M.
  specializes F1 x. specializes F2 x. unfold map_union in M. 
  apply Mem_app_or. destruct~ (f1 x).
Qed.

End MapOps.


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

(* ---------------------------------------------------------------------- *)
(** Definitions *)

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

Implicit Arguments fmap_make [A B]. 


(* ---------------------------------------------------------------------- *)
(** 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.

Implicit Arguments fmap_empty [[A] [B]]. 

(** Singleton fmap *)

Program Definition fmap_single A B (x:A) (v:B) : fmap A B := 
  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 *)

Definition fmap_update A B (h:fmap A B) (x:A) (v:B) := 
  fmap_union (fmap_single x v) h. 
  (* Note: the union operation first reads in the first argument. *)


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

(** Inhabited type [fmap] *)

Global Instance Inhab_fmap A B : Inhab (fmap A B).
Proof using. intros. applys prove_Inhab (@fmap_empty A B). Qed.

(** 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) :=
     fmap_disjoint h1 h2 
  /\ fmap_disjoint h2 h3 
  /\ 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
235
Lemma fmap_make_eq : forall (f1 f2:map A B) F1 F2,
charguer's avatar
charguer committed
236 237 238 239 240 241 242
  (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
243 244 245 246 247
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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

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

Lemma fmap_disjoint_sym : forall h1 h2,
  \# h1 h2 -> \# h2 h1.
Proof using. intros [f1 F1] [f2 F2]. apply map_disjoint_sym. Qed.

Lemma fmap_disjoint_comm : forall h1 h2,
  \# h1 h2 = \# h2 h1.
Proof using. lets: fmap_disjoint_sym. extens*. Qed. 

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].
  split; intros x; specializes M x; 
   destruct (f2 x); intuition; tryfalse.
  intros x. specializes M1 x. specializes M2 x. 
   destruct (f2 x); intuition.
Qed.

Lemma fmap_disjoint_union_eq_l : forall h1 h2 h3,
  \# (h2 \+ h3) h1 =
  (\# h1 h2 /\ \# h1 h3).
Proof using.
  intros. rewrite fmap_disjoint_comm. 
  apply fmap_disjoint_union_eq_r.
Qed.

Lemma fmap_single_same_loc_disjoint : forall (x:A) (v1 v2:B),
charguer's avatar
charguer committed
292 293
  \# (fmap_single x v1) (fmap_single x v2) -> 
  False.
charguer's avatar
charguer committed
294 295 296 297 298 299 300 301 302
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_new : forall null h v,
charguer's avatar
charguer committed
303
  loc_fresh_exists A ->
charguer's avatar
charguer committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  exists l, \# (fmap_single l v) h /\ l <> null.
Proof using.
  intros null (m&(L&M)) v HF.
  unfold fmap_disjoint, map_disjoint. simpl.
  lets (l&F): (HF (null::L)).
  exists l. split.
  { intros l'. case_if~. 
    { right. applys not_not_elim. intros H. applys F.
      constructor. applys~ M. } }
  { intro_subst. applys~ F. }
Qed.

Lemma fmap_disjoint_single_set : forall h l v1 v2,
  \# (fmap_single l v1) h ->
  \# (fmap_single l v2) h.
Proof using.
  introv M. unfolds fmap_disjoint, fmap_single, map_disjoint; simpls.  
  intros l'. specializes M l'. case_if~. destruct M; auto_false.
Qed.


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

charguer's avatar
charguer committed
328
Lemma fmap_union_self : forall h,
charguer's avatar
charguer committed
329 330
  h \+ h = h.
Proof using.
charguer's avatar
charguer committed
331
  intros [f F]. apply~ fmap_make_eq. simpl. intros x.
charguer's avatar
charguer committed
332 333 334 335 336 337 338
  unfold map_union. cases~ (f x).
Qed.

Lemma fmap_union_empty_l : forall h,
  fmap_empty \+ h = h.
Proof using. 
  intros [f F]. unfold fmap_union, map_union, fmap_empty. simpl.
charguer's avatar
charguer committed
339
  apply~ fmap_make_eq.
charguer's avatar
charguer committed
340 341 342 343 344 345
Qed.

Lemma fmap_union_empty_r : forall h,
  h \+ fmap_empty = h.
Proof using. 
  intros [f F]. unfold fmap_union, map_union, fmap_empty. simpl.
charguer's avatar
charguer committed
346
  apply fmap_make_eq. intros x. destruct~ (f x).
charguer's avatar
charguer committed
347 348 349 350 351 352 353
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.
charguer's avatar
charguer committed
354
  applys fmap_make_eq. intros l. 
charguer's avatar
charguer committed
355 356 357 358 359 360 361 362 363 364
  unfolds map_union. 
  lets: func_same_1 l M.
  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.
charguer's avatar
charguer committed
365
  applys fmap_make_eq. intros l. 
charguer's avatar
charguer committed
366 367 368 369 370
  unfolds map_union. 
  lets: func_same_1 l M.
  cases (f1 l); auto_false.
Qed.

charguer's avatar
charguer committed
371
Lemma fmap_union_comm_of_disjoint : forall h1 h2,
charguer's avatar
charguer committed
372 373 374
  \# h1 h2 ->
  h1 \+ h2 = h2 \+ h1.
Proof using.
charguer's avatar
charguer committed
375
  intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl.
charguer's avatar
charguer committed
376 377 378
  intros. rewrite~ map_union_comm.
Qed.

charguer's avatar
charguer committed
379
Lemma fmap_union_comm_of_agree : forall h1 h2,
charguer's avatar
charguer committed
380 381 382
  fmap_agree h1 h2 ->
  h1 \+ h2 = h2 \+ h1.
Proof using.
charguer's avatar
charguer committed
383
  intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl.
charguer's avatar
charguer committed
384 385 386 387 388 389 390 391
  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
392
  apply fmap_make_eq. intros x. unfold map_union. destruct~ (f1 x).
charguer's avatar
charguer committed
393 394 395
Qed.

(*
charguer's avatar
charguer committed
396
Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1' : fmap,
charguer's avatar
charguer committed
397 398 399 400 401 402
  \# 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
403 404
  applys fmap_make_eq. intros x. specializes D x. specializes D' x.
  lets E': fmap_eq_inv_fmap_data_eq (rm E) x. simpls.
charguer's avatar
charguer committed
405 406 407 408 409 410 411 412 413 414 415
  unfolds map_union. 
  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
416
Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1',
charguer's avatar
charguer committed
417 418 419 420 421 422
  \# 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
423 424
  applys fmap_make_eq. intros x. specializes D x. specializes D' x.
  lets E': fmap_eq_inv_fmap_data_eq (rm E) x. simpls.
charguer's avatar
charguer committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  unfolds map_union. 
  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.
  specializes M l E1. 
Qed.

charguer's avatar
charguer committed
449
Lemma fmap_agree_of_disjoint : forall h1 h2,
charguer's avatar
charguer committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 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
  fmap_disjoint h1 h2 ->
  fmap_agree h1 h2.
Proof using. 
  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 ->
  fmap_agree (f1 \+ f2) f3. 
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 ->
  fmap_agree f1 (f2 \+ f3). 
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
494
    try solve [ applys* fmap_agree_of_disjoint ].
charguer's avatar
charguer committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
  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
519
  introv M D. rewrite~ (@fmap_union_comm_of_agree f1 f2) in M.
charguer's avatar
charguer committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  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 ->
     fmap_agree f1 f3 
  /\ fmap_agree f2 f3.
Proof using.
  (* TODO: proofs redundant with others above *)
  introv M2 M1. split.
  { intros l v1 v2 E1 E2. 
    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.
    specializes M1 l. specializes M2 l. 
    unfolds fmap_union, map_union; simpls.
    cases (fmap_data f1 l). (* LATER: name b *)
    { applys eq_trans b. symmetry. applys~ M1. applys~ M2. } 
    { auto. } }
Qed.

Lemma fmap_agree_union_r_inv : forall f1 f2 f3,
  fmap_agree f1 (f2 \+ f3) ->
  fmap_agree f2 f3 ->
     fmap_agree f1 f2 
  /\ fmap_agree f1 f3.
Proof using. 
  hint fmap_agree_sym.
  intros. forwards~ (M1&M2): fmap_agree_union_l_inv f2 f3 f1.
Qed.


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

charguer's avatar
charguer committed
564
Lemma fmap_union_single_l_read : forall f1 f2 l v, 
charguer's avatar
charguer committed
565 566 567 568 569 570
  f1 = fmap_single l v ->
  fmap_data (f1 \+ f2) l = Some v.
Proof using.
  intros. subst. simpl. unfold map_union. case_if~.
Qed.

charguer's avatar
charguer committed
571
Lemma fmap_union_single_to_update : forall f1 f1' f2 l v v', 
charguer's avatar
charguer committed
572 573 574 575 576 577
  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.
  rewrite <- fmap_union_assoc. fequals. 
charguer's avatar
charguer committed
578
  applys fmap_make_eq. intros l'.
charguer's avatar
charguer committed
579 580 581 582 583 584
  unfolds map_union, fmap_single; simpl. case_if~.
Qed.

End FmapProp.

Implicit Arguments fmap_union_assoc [A B].
charguer's avatar
charguer committed
585 586
Implicit Arguments fmap_union_comm_of_disjoint [A B].
Implicit Arguments fmap_union_comm_of_agree [A B].
charguer's avatar
charguer committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659


(* ********************************************************************** *)
(* * 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.

Hint Rewrite 
  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" :=
  match goal with 
  | |- \# _ _ => 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. }
Qed. 


(* ---------------------------------------------------------------------- *)
(* ** 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.

(** [fmap_eq] proves equalities between unions of fmaps.
    It attempts to discharge the disjointness side-conditions.
    Disclaimer: cancels heaps at depth up to 4, but no more. *)

Lemma fmap_union_eq_cancel_1 : forall h1 h2 h2',
  h2 = h2' ->
  h1 \+ h2 = h1 \+ h2'.
Proof using. intros. subst. auto. Qed.  

Lemma fmap_union_eq_cancel_1' : forall h1,
  h1 = h1.
Proof using. intros. auto. Qed.  

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
660
  rewrite (fmap_union_comm_of_disjoint h1 h1').
charguer's avatar
charguer committed
661 662 663 664 665 666 667 668
  rewrite~ fmap_union_assoc. auto.
Qed.  

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
669
  intros. subst. apply~ fmap_union_comm_of_disjoint.
charguer's avatar
charguer committed
670 671 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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
Qed.  

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'.
Qed.  

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.
Qed.  

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'.
Qed.  

End StateEq.

Hint Rewrite 
  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" :=
  subst; 
  rew_fmap; 
  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. }
Qed. 


(* ---------------------------------------------------------------------- *)
(* ** Tactic [fmap_red] for proving [red] goals 
      (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
777 778 779 780 781 782 783 784 785 786
    attempting to solve them using the tactic [fmap_eq].
    The tactic should be configured depending on [red].
    For example:
    
       Ltac fmap_red_base tt :=
        match goal with H: red _ ?t _ _ |- red _ ?t _ _ =>
          applys_eq H 2 4; try fmap_eq end.
 
    The default implementation is a dummy one.
*)
charguer's avatar
charguer committed
787 788 789 790 791 792 793 794 795 796 797

Ltac fmap_red_base tt := fail.

Tactic Notation "fmap_red" := 
  fmap_red_base tt.

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

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