Fmap.v 23.2 KB
 Jacques-Henri Jourdan committed Feb 07, 2018 1 ``````(** `````` charguer committed Mar 22, 2017 2 `````` `````` Jacques-Henri Jourdan committed Feb 07, 2018 3 ``````This file contains a representation of finite maps `````` charguer committed Mar 22, 2017 4 5 ``````that may be used for representing a store. It also provides lemmas and tactics for reasoning about `````` Jacques-Henri Jourdan committed Feb 07, 2018 6 ``````operations on the store (read, write, union). `````` charguer committed Mar 22, 2017 7 8 9 10 11 12 13 `````` 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 `````` (* ********************************************************************** *) (** * 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 43 `````` exists (L : list A), forall (x:A), f x <> None -> mem x L. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 50 ``````(** Compatibility of two partial functions on the intersection `````` charguer committed Mar 22, 2017 51 52 53 `````` of their domains *) Definition map_agree (A B : Type) (f1 f2 : map A B) := `````` Jacques-Henri Jourdan committed Feb 07, 2018 54 `````` forall x v1 v2, `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 69 ``````Lemma map_disjoint_sym : `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 78 `````` map_disjoint f1 f2 -> `````` charguer committed Mar 22, 2017 79 `````` map_union f1 f2 = map_union f2 f1. `````` Jacques-Henri Jourdan committed Feb 07, 2018 80 ``````Proof using. `````` charguer committed Mar 22, 2017 81 82 `````` introv H. unfold map. extens. intros x. unfolds map_disjoint, map_union. `````` Jacques-Henri Jourdan committed Feb 07, 2018 83 `````` specializes H x. cases (f1 x); cases (f2 x); auto. destruct H; false. `````` charguer committed Mar 22, 2017 84 85 86 87 88 ``````Qed. (** Finiteness of union *) Lemma map_union_finite : forall f1 f2, `````` Jacques-Henri Jourdan committed Feb 07, 2018 89 90 `````` map_finite f1 -> map_finite f2 -> `````` charguer committed Mar 22, 2017 91 92 93 `````` map_finite (map_union f1 f2). Proof using. introv [L1 F1] [L2 F2]. exists (L1 ++ L2). introv M. `````` charguer committed Dec 01, 2017 94 95 `````` specializes F1 x. specializes F2 x. unfold map_union in M. apply mem_app. destruct~ (f1 x). `````` charguer committed Mar 22, 2017 96 97 98 99 100 101 102 103 104 ``````Qed. End MapOps. (* ********************************************************************** *) (** * Finite maps *) (* ---------------------------------------------------------------------- *) `````` charguer committed May 16, 2017 105 ``````(** Definition of the type of finite maps *) `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 111 ``````Arguments fmap_make [A] [B]. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 123 ``````Arguments fmap_empty {A} {B}. `````` charguer committed Mar 22, 2017 124 125 126 `````` (** Singleton fmap *) `````` Jacques-Henri Jourdan committed Feb 07, 2018 127 ``````Program Definition fmap_single A B (x:A) (v:B) : fmap A B := `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 146 147 ``````Definition fmap_update A B (h:fmap A B) (x:A) (v:B) := fmap_union (fmap_single x v) h. `````` charguer committed Mar 22, 2017 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 committed Dec 01, 2017 157 ``````Proof using. intros. applys Inhab_of_val (@fmap_empty A B). Qed. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 175 176 `````` fmap_disjoint h1 h2 /\ fmap_disjoint h2 h3 `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 195 ``````Lemma fmap_make_eq : forall (f1 f2:map A B) F1 F2, `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 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 committed Mar 22, 2017 208 209 210 211 212 `````` (* ---------------------------------------------------------------------- *) (* ** Disjointness *) Lemma fmap_disjoint_sym : forall h1 h2, `````` Jacques-Henri Jourdan committed Feb 07, 2018 213 `````` \# h1 h2 -> `````` charguer committed Jun 21, 2017 214 `````` \# h2 h1. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 219 ``````Proof using. lets: fmap_disjoint_sym. extens*. Qed. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 238 `````` split; intros x; specializes M x; `````` charguer committed Mar 22, 2017 239 `````` destruct (f2 x); intuition; tryfalse. `````` Jacques-Henri Jourdan committed Feb 07, 2018 240 `````` intros x. specializes M1 x. specializes M2 x. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 248 `````` intros. rewrite fmap_disjoint_comm. `````` charguer committed Mar 22, 2017 249 250 251 `````` apply fmap_disjoint_union_eq_r. Qed. `````` charguer committed Mar 28, 2017 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 committed Mar 28, 2017 259 ``````Lemma fmap_disjoint_single_single_same_inv : forall (x:A) (v1 v2:B), `````` Jacques-Henri Jourdan committed Feb 07, 2018 260 `````` \# (fmap_single x v1) (fmap_single x v2) -> `````` charguer committed Mar 22, 2017 261 `````` False. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 274 `````` introv M. unfolds fmap_disjoint, fmap_single, map_disjoint; simpls. `````` charguer committed Mar 22, 2017 275 276 277 278 279 280 281 `````` intros l'. specializes M l'. case_if~. destruct M; auto_false. Qed. (* ---------------------------------------------------------------------- *) (* ** Union *) `````` charguer committed Mar 22, 2017 282 ``````Lemma fmap_union_self : forall h, `````` charguer committed Mar 22, 2017 283 284 `````` h \+ h = h. Proof using. `````` charguer committed Mar 22, 2017 285 `````` intros [f F]. apply~ fmap_make_eq. simpl. intros x. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 291 ``````Proof using. `````` charguer committed Mar 22, 2017 292 `````` intros [f F]. unfold fmap_union, map_union, fmap_empty. simpl. `````` charguer committed Mar 22, 2017 293 `````` apply~ fmap_make_eq. `````` charguer committed Mar 22, 2017 294 295 296 297 ``````Qed. Lemma fmap_union_empty_r : forall h, h \+ fmap_empty = h. `````` Jacques-Henri Jourdan committed Feb 07, 2018 298 ``````Proof using. `````` charguer committed Mar 22, 2017 299 `````` intros [f F]. unfold fmap_union, map_union, fmap_empty. simpl. `````` charguer committed Mar 22, 2017 300 `````` apply fmap_make_eq. intros x. destruct~ (f x). `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 308 `````` applys fmap_make_eq. intros l. `````` charguer committed Dec 01, 2017 309 310 `````` unfolds map_union. lets: fun_eq_1 l M. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 319 320 `````` applys fmap_make_eq. intros l. unfolds map_union. `````` charguer committed Dec 01, 2017 321 `````` lets: fun_eq_1 l M. `````` charguer committed Mar 22, 2017 322 323 324 `````` cases (f1 l); auto_false. Qed. `````` charguer committed Mar 22, 2017 325 ``````Lemma fmap_union_comm_of_disjoint : forall h1 h2, `````` charguer committed Mar 22, 2017 326 327 328 `````` \# h1 h2 -> h1 \+ h2 = h2 \+ h1. Proof using. `````` charguer committed Mar 22, 2017 329 `````` intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl. `````` charguer committed Mar 22, 2017 330 331 332 `````` intros. rewrite~ map_union_comm. Qed. `````` charguer committed Mar 22, 2017 333 ``````Lemma fmap_union_comm_of_agree : forall h1 h2, `````` charguer committed Mar 22, 2017 334 335 336 `````` fmap_agree h1 h2 -> h1 \+ h2 = h2 \+ h1. Proof using. `````` charguer committed Mar 22, 2017 337 `````` intros [f1 F1] [f2 F2] H. simpls. apply fmap_make_eq. simpl. `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 346 `````` apply fmap_make_eq. intros x. unfold map_union. destruct~ (f1 x). `````` charguer committed Mar 22, 2017 347 348 349 ``````Qed. (* `````` charguer committed Mar 22, 2017 350 ``````Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1' : fmap, `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 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 committed Feb 07, 2018 359 `````` unfolds map_union. `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 370 ``````Lemma fmap_union_eq_inv_of_disjoint : forall h2 h1 h1', `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 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 committed Feb 07, 2018 379 `````` unfolds map_union. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 400 `````` specializes M l E1. `````` charguer committed Mar 22, 2017 401 402 ``````Qed. `````` charguer committed Mar 22, 2017 403 ``````Lemma fmap_agree_of_disjoint : forall h1 h2, `````` charguer committed Mar 22, 2017 404 405 `````` fmap_disjoint h1 h2 -> fmap_agree h1 h2. `````` Jacques-Henri Jourdan committed Feb 07, 2018 406 ``````Proof using. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 423 `````` fmap_agree (f1 \+ f2) f3. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 435 `````` fmap_agree f1 (f2 \+ f3). `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 448 `````` try solve [ applys* fmap_agree_of_disjoint ]. `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 473 `````` introv M D. rewrite~ (@fmap_union_comm_of_agree f1 f2) in M. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 488 `````` fmap_agree f1 f3 `````` charguer committed Mar 22, 2017 489 490 `````` /\ fmap_agree f2 f3. Proof using. `````` charguer committed Mar 09, 2018 491 `````` (* LATER: proofs redundant with others above *) `````` charguer committed Mar 22, 2017 492 `````` introv M2 M1. split. `````` Jacques-Henri Jourdan committed Feb 07, 2018 493 `````` { intros l v1 v2 E1 E2. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 497 `````` specializes M1 l. specializes M2 l. `````` charguer committed Mar 22, 2017 498 499 `````` unfolds fmap_union, map_union; simpls. cases (fmap_data f1 l). (* LATER: name b *) `````` Jacques-Henri Jourdan committed Feb 07, 2018 500 `````` { applys eq_trans b. symmetry. applys~ M1. applys~ M2. } `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 507 `````` fmap_agree f1 f2 `````` charguer committed Mar 22, 2017 508 `````` /\ fmap_agree f1 f3. `````` Jacques-Henri Jourdan committed Feb 07, 2018 509 ``````Proof using. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 518 ``````Lemma fmap_union_single_l_read : forall f1 f2 l v, `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 525 ``````Lemma fmap_union_single_to_update : forall f1 f1' f2 l v v', `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 531 `````` rewrite <- fmap_union_assoc. fequals. `````` charguer committed Mar 22, 2017 532 `````` applys fmap_make_eq. intros l'. `````` charguer committed Mar 22, 2017 533 534 535 536 537 `````` unfolds map_union, fmap_single; simpl. case_if~. Qed. End FmapProp. `````` charguer committed Dec 01, 2017 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 committed Mar 22, 2017 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 committed Feb 07, 2018 556 ``````Hint Rewrite `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 570 `````` match goal with `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 584 ``````Qed. `````` charguer committed Mar 22, 2017 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 committed May 16, 2017 595 596 ``````(** [fmap_eq] proves equalities between unions of fmaps, of the form [h1 \+ h2 \+ h3 \+ h4 = h1' \+ h2' \+ h3' \+ h4'] `````` charguer committed Mar 22, 2017 597 `````` It attempts to discharge the disjointness side-conditions. `````` charguer committed May 16, 2017 598 `````` Disclaimer: it cancels heaps at depth up to 4, but no more. *) `````` charguer committed Mar 22, 2017 599 600 601 602 `````` Lemma fmap_union_eq_cancel_1 : forall h1 h2 h2', h2 = h2' -> h1 \+ h2 = h1 \+ h2'. `````` Jacques-Henri Jourdan committed Feb 07, 2018 603 ``````Proof using. intros. subst. auto. Qed. `````` charguer committed Mar 22, 2017 604 605 606 `````` Lemma fmap_union_eq_cancel_1' : forall h1, h1 = h1. `````` Jacques-Henri Jourdan committed Feb 07, 2018 607 ``````Proof using. intros. auto. Qed. `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 615 `````` rewrite (fmap_union_comm_of_disjoint h1 h1'). `````` charguer committed Mar 22, 2017 616 `````` rewrite~ fmap_union_assoc. auto. `````` Jacques-Henri Jourdan committed Feb 07, 2018 617 ``````Qed. `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 624 `````` intros. subst. apply~ fmap_union_comm_of_disjoint. `````` Jacques-Henri Jourdan committed Feb 07, 2018 625 ``````Qed. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 646 ``````Qed. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 657 ``````Qed. `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 667 ``````Qed. `````` charguer committed Mar 22, 2017 668 669 670 `````` End StateEq. `````` Jacques-Henri Jourdan committed Feb 07, 2018 671 ``````Hint Rewrite `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 698 699 `````` subst; rew_fmap; `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 721 ``````Qed. `````` charguer committed Mar 22, 2017 722 723 724 `````` (* ---------------------------------------------------------------------- *) `````` Jacques-Henri Jourdan committed Feb 07, 2018 725 ``````(* ** Tactic [fmap_red] for proving [red] goals `````` charguer committed Mar 22, 2017 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 committed Mar 22, 2017 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 committed Feb 07, 2018 735 `````` `````` charguer committed Mar 22, 2017 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 committed Feb 07, 2018 739 `````` `````` charguer committed Mar 22, 2017 740 741 `````` The default implementation is a dummy one. *) `````` charguer committed Mar 22, 2017 742 743 744 `````` Ltac fmap_red_base tt := fail. `````` Jacques-Henri Jourdan committed Feb 07, 2018 745 ``````Tactic Notation "fmap_red" := `````` charguer committed Mar 22, 2017 746 747 `````` fmap_red_base tt. `````` Jacques-Henri Jourdan committed Feb 07, 2018 748 ``````Tactic Notation "fmap_red" "~" := `````` charguer committed Mar 22, 2017 749 750 `````` fmap_red; auto_tilde. `````` Jacques-Henri Jourdan committed Feb 07, 2018 751 ``````Tactic Notation "fmap_red" "*" := `````` charguer committed Mar 22, 2017 752 `````` fmap_red; auto_star. `````` charguer committed Mar 28, 2017 753 754 755 756 757 758 759 760 `````` (* ********************************************************************** *) (** * Consecutive locations and fresh locations *) (* ---------------------------------------------------------------------- *) (** ** Existence of fresh locations *) `````` charguer committed Mar 31, 2017 761 ``````Fixpoint fmap_conseq (B:Type) (l:nat) (k:nat) (v:B) : fmap nat B := `````` charguer committed Mar 28, 2017 762 `````` match k with `````` charguer committed Mar 31, 2017 763 764 `````` | O => fmap_empty | S k' => (fmap_single l v) \+ (fmap_conseq (S l) k' v) `````` charguer committed Mar 28, 2017 765 766 `````` end. `````` Jacques-Henri Jourdan committed Feb 07, 2018 767 ``````Lemma fmap_conseq_zero : forall B (l:nat) (v:B), `````` charguer committed Mar 28, 2017 768 769 770 `````` fmap_conseq l O v = fmap_empty. Proof using. auto. Qed. `````` Jacques-Henri Jourdan committed Feb 07, 2018 771 ``````Lemma fmap_conseq_succ : forall B (l:nat) (k:nat) (v:B), `````` charguer committed Mar 31, 2017 772 `````` fmap_conseq l (S k) v = (fmap_single l v) \+ (fmap_conseq (S l) k v). `````` charguer committed Mar 28, 2017 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 committed Feb 07, 2018 788 `````` mem l L -> `````` charguer committed Mar 28, 2017 789 790 791 `````` (l < loc_fresh_gen L)%nat. Proof using. intros l L. unfold loc_fresh_gen. `````` Jacques-Henri Jourdan committed Feb 07, 2018 792 `````` induction L; introv M; inverts M; rew_listx. `````` charguer committed Mar 28, 2017 793 794 795 796 797 `````` { math. } { forwards~: IHL. math. } Qed. Lemma loc_fresh_nat_ge : forall (L:list nat), `````` charguer committed Dec 01, 2017 798 `````` exists (l:nat), forall (i:nat), ~ mem (l+i)%nat L. `````` charguer committed Mar 28, 2017 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 committed Dec 01, 2017 805 `````` exists (l:nat), ~ mem l L. `````` charguer committed Mar 28, 2017 806 ``````Proof using. `````` Jacques-Henri Jourdan committed Feb 07, 2018 807 `````` intros L. forwards (l&P): loc_fresh_nat_ge L. `````` charguer committed Mar 28, 2017 808 809 810 811 812 `````` exists l. intros M. applys (P 0%nat). applys_eq M 2. math. Qed. (* ---------------------------------------------------------------------- *) `````` charguer committed May 16, 2017 813 ``````(** ** Extension of a number of consecutive fresh locations *) `````` charguer committed Mar 28, 2017 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 committed Mar 09, 2018 826 `````` { intros l'. case_if~. (* --LATER: fix TLC substitution in case_if *) `````` charguer committed Dec 01, 2017 827 `````` { subst. right. applys not_not_inv. intros H. applys F. `````` charguer committed Mar 28, 2017 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 committed Mar 31, 2017 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 committed Mar 09, 2018 844 `````` { simpl. unfold map_union. case_if~. `````` charguer committed Dec 01, 2017 845 `````` { subst. right. applys not_not_inv. intros H. applys F 0%nat. `````` charguer committed Mar 31, 2017 846 `````` constructor. math_rewrite (l'+0 = l')%nat. applys~ M. } } `````` charguer committed Mar 28, 2017 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 committed Mar 31, 2017 852 853 `````` (l < l')%nat \/ (l >= l'+k)%nat -> \# (fmap_single l v) (fmap_conseq l' k v). `````` charguer committed Mar 28, 2017 854 ``````Proof using. `````` charguer committed Mar 31, 2017 855 `````` introv N. gen l'. induction k; intros. `````` charguer committed Mar 28, 2017 856 857 `````` { rewrite~ fmap_conseq_zero. } { rewrite fmap_conseq_succ. rew_disjoint. split. `````` charguer committed Mar 31, 2017 858 859 `````` { applys fmap_disjoint_single_single. destruct N; math. } { applys IHk. destruct N. { left; math. } { right; math. } } } `````` charguer committed Mar 28, 2017 860 861 862 ``````Qed. End FmapFresh.``````