Fmap.v 23.4 KB
 charguer committed Mar 22, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 ``````(** 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. `````` charguer committed Dec 04, 2017 14 ``````From TLC Require Import LibCore. `````` charguer committed Mar 22, 2017 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 45 46 47 48 `````` (* ********************************************************************** *) Tactic Notation "cases" constr(E) := (* TODO For TLC *) let H := fresh "Eq" in cases E as H. (* ********************************************************************** *) (** * 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 committed Dec 01, 2017 49 `````` exists (L : list A), forall (x:A), f x <> None -> mem x L. `````` charguer committed Mar 22, 2017 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 `````` (** 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. `````` charguer committed Dec 01, 2017 100 101 `````` specializes F1 x. specializes F2 x. unfold map_union in M. apply mem_app. destruct~ (f1 x). `````` charguer committed Mar 22, 2017 102 103 104 105 106 107 108 109 110 ``````Qed. End MapOps. (* ********************************************************************** *) (** * Finite maps *) (* ---------------------------------------------------------------------- *) `````` charguer committed May 16, 2017 111 ``````(** Definition of the type of finite maps *) `````` charguer committed Mar 22, 2017 112 113 114 115 116 `````` Inductive fmap (A B : Type) : Type := fmap_make { fmap_data :> map A B; fmap_finite : map_finite fmap_data }. `````` charguer committed Dec 01, 2017 117 ``````Arguments fmap_make [A] [B]. `````` charguer committed Mar 22, 2017 118 119 120 121 122 123 124 125 126 127 128 `````` (* ---------------------------------------------------------------------- *) (** 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. `````` charguer committed Dec 01, 2017 129 ``````Arguments fmap_empty {A} {B}. `````` charguer committed Mar 22, 2017 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 `````` (** 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). `````` charguer committed Dec 01, 2017 163 ``````Proof using. intros. applys Inhab_of_val (@fmap_empty A B). Qed. `````` charguer committed Mar 22, 2017 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 `````` (** 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 committed Mar 22, 2017 201 ``````Lemma fmap_make_eq : forall (f1 f2:map A B) F1 F2, `````` charguer committed Mar 22, 2017 202 203 204 205 206 207 208 `````` (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 committed Mar 22, 2017 209 210 211 212 213 ``````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 committed Mar 22, 2017 214 215 216 217 218 `````` (* ---------------------------------------------------------------------- *) (* ** Disjointness *) Lemma fmap_disjoint_sym : forall h1 h2, `````` charguer committed Jun 21, 2017 219 220 `````` \# h1 h2 -> \# h2 h1. `````` charguer committed Mar 22, 2017 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 ``````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. `````` charguer committed Mar 28, 2017 258 259 260 261 262 263 264 ``````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 committed Mar 28, 2017 265 ``````Lemma fmap_disjoint_single_single_same_inv : forall (x:A) (v1 v2:B), `````` charguer committed Mar 22, 2017 266 267 `````` \# (fmap_single x v1) (fmap_single x v2) -> False. `````` charguer committed Mar 22, 2017 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 ``````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. introv M. unfolds fmap_disjoint, fmap_single, map_disjoint; simpls. intros l'. specializes M l'. case_if~. destruct M; auto_false. Qed. (* ---------------------------------------------------------------------- *) (* ** Union *) `````` charguer committed Mar 22, 2017 288 ``````Lemma fmap_union_self : forall h, `````` charguer committed Mar 22, 2017 289 290 `````` h \+ h = h. Proof using. `````` charguer committed Mar 22, 2017 291 `````` intros [f F]. apply~ fmap_make_eq. simpl. intros x. `````` charguer committed Mar 22, 2017 292 293 294 295 296 297 298 `````` 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 committed Mar 22, 2017 299 `````` apply~ fmap_make_eq. `````` charguer committed Mar 22, 2017 300 301 302 303 304 305 ``````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 committed Mar 22, 2017 306 `````` apply fmap_make_eq. intros x. destruct~ (f x). `````` charguer committed Mar 22, 2017 307 308 309 310 311 312 313 ``````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 committed Mar 22, 2017 314 `````` applys fmap_make_eq. intros l. `````` charguer committed Dec 01, 2017 315 316 `````` unfolds map_union. lets: fun_eq_1 l M. `````` charguer committed Mar 22, 2017 317 318 319 320 321 322 323 324 `````` 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 committed Mar 22, 2017 325 `````` applys fmap_make_eq. intros l. `````` charguer committed Mar 22, 2017 326 `````` unfolds map_union. `````` charguer committed Dec 01, 2017 327 `````` lets: fun_eq_1 l M. `````` charguer committed Mar 22, 2017 328 329 330 `````` cases (f1 l); auto_false. Qed. `````` charguer committed Mar 22, 2017 331 ``````Lemma fmap_union_comm_of_disjoint : forall h1 h2, `````` charguer committed Mar 22, 2017 332 333 334 `````` \# h1 h2 -> h1 \+ h2 = h2 \+ h1. Proof using. `````` charguer committed Mar 22, 2017 335 `````` intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl. `````` charguer committed Mar 22, 2017 336 337 338 `````` intros. rewrite~ map_union_comm. Qed. `````` charguer committed Mar 22, 2017 339 ``````Lemma fmap_union_comm_of_agree : forall h1 h2, `````` charguer committed Mar 22, 2017 340 341 342 `````` fmap_agree h1 h2 -> h1 \+ h2 = h2 \+ h1. Proof using. `````` charguer committed Mar 22, 2017 343 `````` intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl. `````` charguer committed Mar 22, 2017 344 345 346 347 348 349 350 351 `````` 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 committed Mar 22, 2017 352 `````` apply fmap_make_eq. intros x. unfold map_union. destruct~ (f1 x). `````` charguer committed Mar 22, 2017 353 354 355 ``````Qed. (* `````` charguer committed Mar 22, 2017 356 ``````Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1' : fmap, `````` charguer committed Mar 22, 2017 357 358 359 360 361 362 `````` \# 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 committed Mar 22, 2017 363 364 `````` 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 committed Mar 22, 2017 365 366 367 368 369 370 371 372 373 374 375 `````` 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 committed Mar 22, 2017 376 ``````Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1', `````` charguer committed Mar 22, 2017 377 378 379 380 381 382 `````` \# h1 h2 -> \# h1' h2 -> h1 \+ h2 = h1' \+ h2 -> h1 = h1'. Proof using. intros [f2 F2] [f1' F1'] [f1 F1] D D' E. `````` charguer committed Mar 22, 2017 383 384 `````` 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 committed Mar 22, 2017 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 `````` 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 committed Mar 22, 2017 409 ``````Lemma fmap_agree_of_disjoint : forall h1 h2, `````` charguer committed Mar 22, 2017 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 `````` 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 committed Mar 22, 2017 454 `````` try solve [ applys* fmap_agree_of_disjoint ]. `````` charguer committed Mar 22, 2017 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 `````` 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 committed Mar 22, 2017 479 `````` introv M D. rewrite~ (@fmap_union_comm_of_agree f1 f2) in M. `````` charguer committed Mar 22, 2017 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 `````` 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 committed Mar 22, 2017 524 ``````Lemma fmap_union_single_l_read : forall f1 f2 l v, `````` charguer committed Mar 22, 2017 525 526 527 528 529 530 `````` f1 = fmap_single l v -> fmap_data (f1 \+ f2) l = Some v. Proof using. intros. subst. simpl. unfold map_union. case_if~. Qed. `````` charguer committed Mar 22, 2017 531 ``````Lemma fmap_union_single_to_update : forall f1 f1' f2 l v v', `````` charguer committed Mar 22, 2017 532 533 534 535 536 537 `````` 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 committed Mar 22, 2017 538 `````` applys fmap_make_eq. intros l'. `````` charguer committed Mar 22, 2017 539 540 541 542 543 `````` unfolds map_union, fmap_single; simpl. case_if~. Qed. End FmapProp. `````` charguer committed Dec 01, 2017 544 545 546 ``````Arguments fmap_union_assoc [A] [B]. Arguments fmap_union_comm_of_disjoint [A] [B]. Arguments fmap_union_comm_of_agree [A] [B]. `````` charguer committed Mar 22, 2017 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 `````` (* ********************************************************************** *) (* * 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. `````` charguer committed May 16, 2017 601 602 ``````(** [fmap_eq] proves equalities between unions of fmaps, of the form [h1 \+ h2 \+ h3 \+ h4 = h1' \+ h2' \+ h3' \+ h4'] `````` charguer committed Mar 22, 2017 603 `````` It attempts to discharge the disjointness side-conditions. `````` charguer committed May 16, 2017 604 `````` Disclaimer: it cancels heaps at depth up to 4, but no more. *) `````` charguer committed Mar 22, 2017 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 `````` 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 committed Mar 22, 2017 621 `````` rewrite (fmap_union_comm_of_disjoint h1 h1'). `````` charguer committed Mar 22, 2017 622 623 624 625 626 627 628 629 `````` 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 committed Mar 22, 2017 630 `````` intros. subst. apply~ fmap_union_comm_of_disjoint. `````` charguer committed Mar 22, 2017 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 660 661 662 663 664 665 666 667 668 669 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 ``````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 committed Mar 22, 2017 738 739 740 741 742 743 744 745 746 747 `````` 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 committed Mar 22, 2017 748 749 750 751 752 753 754 755 756 757 758 `````` 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. `````` charguer committed Mar 28, 2017 759 760 761 762 763 764 765 766 `````` (* ********************************************************************** *) (** * Consecutive locations and fresh locations *) (* ---------------------------------------------------------------------- *) (** ** Existence of fresh locations *) `````` charguer committed Mar 31, 2017 767 ``````Fixpoint fmap_conseq (B:Type) (l:nat) (k:nat) (v:B) : fmap nat B := `````` charguer committed Mar 28, 2017 768 `````` match k with `````` charguer committed Mar 31, 2017 769 770 `````` | O => fmap_empty | S k' => (fmap_single l v) \+ (fmap_conseq (S l) k' v) `````` charguer committed Mar 28, 2017 771 772 773 774 775 776 777 `````` end. Lemma fmap_conseq_zero : forall B (l:nat) (v:B), fmap_conseq l O v = fmap_empty. Proof using. auto. Qed. Lemma fmap_conseq_succ : forall B (l:nat) (k:nat) (v:B), `````` charguer committed Mar 31, 2017 778 `````` fmap_conseq l (S k) v = (fmap_single l v) \+ (fmap_conseq (S l) k v). `````` charguer committed Mar 28, 2017 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 ``````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, `````` charguer committed Dec 01, 2017 794 `````` mem l L -> `````` charguer committed Mar 28, 2017 795 796 797 `````` (l < loc_fresh_gen L)%nat. Proof using. intros l L. unfold loc_fresh_gen. `````` charguer committed Dec 01, 2017 798 `````` induction L; introv M; inverts M; rew_listx. `````` charguer committed Mar 28, 2017 799 800 801 802 803 `````` { math. } { forwards~: IHL. math. } Qed. Lemma loc_fresh_nat_ge : forall (L:list nat), `````` charguer committed Dec 01, 2017 804 `````` exists (l:nat), forall (i:nat), ~ mem (l+i)%nat L. `````` charguer committed Mar 28, 2017 805 806 807 808 809 810 ``````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 committed Dec 01, 2017 811 `````` exists (l:nat), ~ mem l L. `````` charguer committed Mar 28, 2017 812 813 814 815 816 817 818 ``````Proof using. intros L. forwards (l&P): loc_fresh_nat_ge L. exists l. intros M. applys (P 0%nat). applys_eq M 2. math. Qed. (* ---------------------------------------------------------------------- *) `````` charguer committed May 16, 2017 819 ``````(** ** Extension of a number of consecutive fresh locations *) `````` charguer committed Mar 28, 2017 820 821 822 823 824 825 826 827 828 829 830 831 `````` 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 committed Dec 01, 2017 832 833 `````` { intros l'. case_if~. (* --TODO: fix subst *) { subst. right. applys not_not_inv. intros H. applys F. `````` charguer committed Mar 28, 2017 834 835 836 837 838 839 840 841 842 843 844 `````` 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 committed Mar 31, 2017 845 846 847 848 849 `````` { 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 committed Dec 01, 2017 850 851 `````` { simpl. unfold map_union. case_if~. (* --TODO: fix subst *) { subst. right. applys not_not_inv. intros H. applys F 0%nat. `````` charguer committed Mar 31, 2017 852 `````` constructor. math_rewrite (l'+0 = l')%nat. applys~ M. } } `````` charguer committed Mar 28, 2017 853 854 855 856 857 `````` { auto. } } } { intro_subst. applys~ F 0%nat. rew_nat. auto. } Qed. Lemma fmap_disjoint_single_conseq : forall B l l' k (v:B), `````` charguer committed Mar 31, 2017 858 859 `````` (l < l')%nat \/ (l >= l'+k)%nat -> \# (fmap_single l v) (fmap_conseq l' k v). `````` charguer committed Mar 28, 2017 860 ``````Proof using. `````` charguer committed Mar 31, 2017 861 `````` introv N. gen l'. induction k; intros. `````` charguer committed Mar 28, 2017 862 863 `````` { rewrite~ fmap_conseq_zero. } { rewrite fmap_conseq_succ. rew_disjoint. split. `````` charguer committed Mar 31, 2017 864 865 `````` { applys fmap_disjoint_single_single. destruct N; math. } { applys IHk. destruct N. { left; math. } { right; math. } } } `````` charguer committed Mar 28, 2017 866 867 868 869 870 871 872 ``````Qed. End FmapFresh. ``````