TypeValidatorScript.sml 34.8 KB
Newer Older
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 45 46 47 48 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 235 236 237 238 239 240 241 242 243 244 245 246 247 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 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 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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 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 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 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 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 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 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
open miscTheory realTheory realLib sptreeTheory;
open ExpressionsTheory MachineTypeTheory FloverTactics ExpressionAbbrevsTheory
     ExpressionSemanticsTheory CommandsTheory FloverMapTheory ResultsTheory;
open ResultsLib;
open preamble;

val _ = new_theory "TypeValidator";

val validTypes_def = Define `
  validTypes e Gamma =
    ? mG. FloverMapTree_find e Gamma = SOME mG /\
    (case e of
    | Var x => T
    | Const m v => m = mG
    | Unop u e1 =>
        validTypes e1 Gamma /\
        (? me. FloverMapTree_find e1 Gamma = SOME me /\ isCompat me mG)
    | Binop b e1 e2 =>
        validTypes e1 Gamma /\
        validTypes e2 Gamma /\
        (?m1 m2. FloverMapTree_find e1 Gamma = SOME m1 /\
                 FloverMapTree_find e2 Gamma = SOME m2 /\
                 isJoin m1 m2 mG)
    | Fma e1 e2 e3 =>
        validTypes e1 Gamma /\
        validTypes e2 Gamma /\
        validTypes e3 Gamma /\
        (?m1 m2 m3. FloverMapTree_find e1 Gamma = SOME m1 /\
                    FloverMapTree_find e2 Gamma = SOME m2 /\
                    FloverMapTree_find e3 Gamma = SOME m3 /\
                    isJoin3 m1 m2 m3 mG)
    | Downcast m e1 =>
        validTypes e1 Gamma /\
        m = mG /\
        (? m1. FloverMapTree_find e1 Gamma = SOME m1 /\
               isMorePrecise m1 mG)) /\
    (! E Gamma2 v m.
      (! e m. FloverMapTree_find e Gamma = SOME m ==>
              FloverMapTree_find e Gamma2 = SOME m) /\
      eval_expr E (toRExpMap Gamma2) e v m ==>
      m = mG)`;

val validTypes_single = store_thm (
  "validTypes_single",
  ``!e Gamma.
      validTypes e Gamma ==>
      ?mG.
        FloverMapTree_find e Gamma = SOME mG /\
        (!E v m Gamma2.
           (! e m. FloverMapTree_find e Gamma = SOME m ==>
                   FloverMapTree_find e Gamma2 = SOME m) /\
           eval_expr E (toRExpMap Gamma2) e v m ==>
           m = mG)``,
  Cases_on `e` \\ rpt strip_tac \\ fs[Once validTypes_def] \\ rpt strip_tac
  \\ first_x_assum irule \\ fs[]
  \\ rpt (asm_exists_tac \\ fs[]));

val validTypes_exec = store_thm (
  "validTypes_exec",
  ``! e Gamma m.
      validTypes e Gamma /\
      FloverMapTree_find e Gamma = SOME m ==>
      ! E v mG.
        eval_expr E (toRExpMap Gamma) e v mG ==>
        mG = m``,
  rpt strip_tac \\ IMP_RES_TAC validTypes_single
  \\ rename1 `FloverMapTree_find e Gamma = SOME mG2`
  \\ `m = mG2`  by (fs[])
  \\ rveq
  \\ first_x_assum irule
  \\ qexistsl_tac [`E`, `Gamma`, `v`] \\ fs[]);

val isMonotone_def = Define `
  isMonotone NONE mNew = T /\
  isMonotone (SOME mOld) mNew = (mOld = mNew)`;

val addMono_def = Define `
  addMono e m map =
    case FloverMapTree_find e map of
    | SOME mOld => Fail "Nonmonotonic map update"
    | NONE => Succes (FloverMapTree_insert e m map)`;

val _ = Parse.temp_overload_on ("insert", Term`FloverMapTree_insert`);

val getValidMap_def = Define `
  getValidMap Gamma e akk =
  if (FloverMapTree_mem e akk)
  then Succes akk
  else
    let mOldO = FloverMapTree_find e Gamma in
    case e of
    | Var x =>
      (case mOldO of
      | SOME m => Succes (insert e m akk)
      | NONE => Fail "Undefined variable")
    | Const m n =>
      if (isMonotone mOldO m)
      then Succes (insert e m akk)
      else Fail "Wrong type annotation for Constant"
    | Unop u e1 =>
    do
      akk_new <- getValidMap Gamma e1 akk;
      case FloverMapTree_find e1 akk_new of
      | NONE => Fail "Cannot typecheck unary op"
      | SOME m_e1 =>
        if (isFixedPoint m_e1)
          then
            case mOldO of
            | NONE => Fail "Undefined fixed-point type"
            | SOME mFix =>
              if (isCompat m_e1 mFix)
              then addMono e mFix akk_new
              else Fail "Incompatible type assignment"
          else
            if (isMonotone mOldO m_e1)
            then addMono e m_e1 akk_new
            else Fail "Wront type annotation for unary constant"
    od
    | Binop b e1 e2 =>
    do
      akk1_map <- getValidMap Gamma e1 akk;
      akk2_map <- getValidMap Gamma e2 akk1_map;
      let m1 = FloverMapTree_find e1 akk2_map;
          m2 = FloverMapTree_find e2 akk2_map in
      case m1, m2 of
      | SOME t1, SOME t2 =>
        if (isFixedPoint t1 /\ isFixedPoint t2)
        then
          case mOldO of
          | NONE => Fail "Undefined fixed-point type"
          | SOME mj =>
            if (morePrecise t1 mj /\ morePrecise t2 mj)
            then addMono e mj akk2_map
            else Fail "Incorrect fixed-point type"
        else
          (case join_fl t1 t2 of
          | NONE => Fail "Could not compute join for arguments"
          | SOME m =>
            if (isMonotone mOldO m)
            then addMono e m akk2_map
            else Fail "Wrong type annotation for binary operator")
      | _, _ => Fail "Could not compute type for arguments"
    od
    | Fma e1 e2 e3 =>
    do
      akk1_map <- getValidMap Gamma e1 akk;
      akk2_map <- getValidMap Gamma e2 akk1_map;
      akk3_map <- getValidMap Gamma e3 akk2_map;
      let m1 = FloverMapTree_find e1 akk3_map;
          m2 = FloverMapTree_find e2 akk3_map;
          m3 = FloverMapTree_find e3 akk3_map in
      case m1, m2, m3 of
      | SOME t1, SOME t2, SOME t3=>
        if (isFixedPoint t1 /\ isFixedPoint t2 /\ isFixedPoint t3)
        then
          case mOldO of
          | NONE => Fail "Undefined fixed-point type"
          | SOME mj =>
            if (morePrecise t1 mj /\ morePrecise t2 mj /\ morePrecise t3 mj)
            then addMono e mj akk3_map
            else Fail "Incorrect fixed-point type"
        else
          (case join_fl3 t1 t2 t3 of
          | NONE => Fail "Could not compute join for arguments"
          | SOME m =>
            if (isMonotone mOldO m)
            then addMono e m akk3_map
            else Fail "Wrong type annotation for FMA")
      | _, _ => Fail "Could not compute type for arguments"
    od
    | Downcast m e1 =>
    do
      akk_new <- getValidMap Gamma e1 akk;
      case FloverMapTree_find e1 akk_new of
      | NONE => Fail "Could not find cast argument type"
      | SOME t1 =>
        if (isFixedPoint t1)
        then
          case mOldO of
          | NONE => Fail "Could not find fixed_point type for cast"
          | SOME mFix =>
            if (m = mFix /\ morePrecise t1 mFix)
            then addMono e mFix akk_new
            else Fail "Incorrect fixed-point type"
        else
          if (morePrecise t1 m /\ isMonotone mOldO m)
          then addMono e m akk_new
          else Fail "Cannot cast down to lower precision"
    od`;

add_unevaluated_function ``getValidMap``;

val tMap_def = FloverMapTree_correct;

val toRExpMap_char = store_thm (
  "toRExpMap_char",
  ``!e m akk.
    toRExpMap (FloverMapTree_insert e m akk) e = SOME m``,
  fs[toRExpMap_def, tMap_def]);

val by_monotonicity =
  once_rewrite_tac [map_find_add]
  \\ COND_CASES_TAC \\ fs[]
  \\ rveq
  \\ fs[FloverMapTree_mem_def]
  \\ EVERY_CASE_TAC \\ fs[];

val getValidMap_mono = store_thm (
  "getValidMap_mono",
  ``! e Gamma akk res.
      getValidMap Gamma e akk = Succes res ==>
      ! e m. FloverMapTree_find e akk = SOME m ==>
        FloverMapTree_find e res = SOME m``,
  Induct_on  `e`
  \\ once_rewrite_tac [getValidMap_def] \\ fs[]
  \\ rpt strip_tac
  >- (Cases_on `FloverMapTree_mem (Var n) akk`
      \\ fs[option_case_eq] \\ rveq \\ fs[]
      \\ by_monotonicity)
  >- (Cases_on `FloverMapTree_mem (Const m v) akk`
      \\ fs[option_case_eq] \\ rveq \\ fs[]
      \\ Cases_on `isMonotone (FloverMapTree_find (Const m v) Gamma) m`
      \\ fs[]
      \\ rveq \\ by_monotonicity)
  >- (Cases_on `FloverMapTree_mem (Unop u e) akk`
      \\ fs[] \\ rveq \\ fs[]
      \\ Cases_on `getValidMap Gamma e akk` \\ fs[option_case_eq]
      \\ Cases_on `isFixedPoint m_e1` \\ fs[option_case_eq]
      >- (Cases_on `isCompat m_e1 mFix` \\ fs[addMono_def, option_case_eq]
          \\ rveq \\ res_tac
          \\ by_monotonicity)
      \\ Cases_on `isMonotone (FloverMapTree_find (Unop u e) Gamma) m_e1`
      \\ fs[addMono_def, option_case_eq] \\ rveq
      \\ res_tac
      \\ by_monotonicity)
  >- (rename1 `Binop b e1 e2` \\ Cases_on `FloverMapTree_mem (Binop b e1 e2) akk`
      \\ fs[] \\ rveq \\ fs[]
      \\ Cases_on `getValidMap Gamma e1 akk` \\ fs[result_bind_def]
      \\ Cases_on `getValidMap Gamma e2 a` \\ fs[option_case_eq]
      \\ Cases_on `isFixedPoint t1` \\ Cases_on `isFixedPoint t2` \\ fs[option_case_eq]
      >- (Cases_on `morePrecise t1 mj` \\ Cases_on `morePrecise t2 mj`
          \\ fs[addMono_def, option_case_eq]
          \\ rveq \\ res_tac
          \\ by_monotonicity)
      \\ rename1 `join_fl t1 t2 = SOME mj`
      \\ Cases_on `isMonotone (FloverMapTree_find (Binop b e1 e2) Gamma) mj`
      \\ fs[addMono_def, option_case_eq] \\ rveq \\ res_tac
      \\ by_monotonicity)
  >- (rename1 `Fma e1 e2 e3` \\ Cases_on `FloverMapTree_mem (Fma e1 e2 e3) akk`
      \\ fs[] \\ rveq \\ fs[]
      \\ Cases_on `getValidMap Gamma e1 akk` \\ fs[result_bind_def]
      \\ Cases_on `getValidMap Gamma e2 a` \\ fs[result_bind_def]
      \\ Cases_on `getValidMap Gamma e3 a'` \\ fs[option_case_eq]
      \\ Cases_on `isFixedPoint t1`
      \\ Cases_on `isFixedPoint t2`
      \\ Cases_on `isFixedPoint t3`
      \\ fs[option_case_eq]
      >- (Cases_on `morePrecise t1 mj`
          \\ Cases_on `morePrecise t2 mj`
          \\ Cases_on `morePrecise t3 mj`
          \\ fs[addMono_def, option_case_eq]
          \\ rveq \\ res_tac
          \\ by_monotonicity)
      \\ rename1 `join_fl3 t1 t2 t3 = SOME mj`
      \\ Cases_on `isMonotone (FloverMapTree_find (Fma e1 e2 e3) Gamma) mj`
      \\ fs[addMono_def, option_case_eq] \\ rveq \\ res_tac
      \\ by_monotonicity)
  \\ Cases_on `FloverMapTree_mem (Downcast m e) akk`
  \\ fs[] \\ rveq \\ fs[]
  \\ Cases_on `getValidMap Gamma e akk` \\ fs[option_case_eq]
  \\ Cases_on `isFixedPoint t1` \\ fs[option_case_eq]
  >- (Cases_on `m = mFix` \\ Cases_on `morePrecise t1 mFix`
      \\ fs[addMono_def, option_case_eq]
      \\ rveq \\ res_tac
      \\ by_monotonicity)
  \\ Cases_on `morePrecise t1 m`
  \\ Cases_on `isMonotone (FloverMapTree_find (Downcast m e) Gamma) m`
  \\ fs[addMono_def, option_case_eq] \\ rveq
  \\ res_tac
  \\ by_monotonicity);

val validTypes_mono = store_thm (
  "validTypes_mono",
  ``! e map1 map2.
      (! e m. FloverMapTree_find e map1 = SOME m ==>
              FloverMapTree_find e map2 = SOME m) /\
      validTypes e map1 ==>
      validTypes e map2``,
  Induct_on `e` \\ fs[Once validTypes_def]
  \\ rpt strip_tac
  \\ simp [Once validTypes_def]
  >- (qexists_tac `mG` \\ fs[]
      \\ rpt strip_tac \\ first_x_assum irule
      \\ qexistsl_tac [`E`, `Gamma2`, `v`]
      \\ fs[])
  >- (rpt strip_tac \\ first_x_assum irule
      \\ qexistsl_tac [`E`, `Gamma2`, `v'`] \\ fs[])
  >- (qexists_tac `mG` \\ fs[]
      \\ rpt conj_tac
      >- (first_x_assum irule
          \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
      >- (qexists_tac `me` \\ fs[])
      \\ rpt strip_tac \\ first_x_assum irule
      \\ qexistsl_tac [`E`, `Gamma2`, `v`] \\ fs[])
  >- (qexists_tac `mG` \\ fs[]
      \\ rpt conj_tac
      >- (first_x_assum irule
          \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
      >- (first_x_assum irule
          \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
      >- (qexistsl_tac [`m1`, `m2`] \\ fs[])
      \\ rpt strip_tac \\ first_x_assum irule
      \\ qexistsl_tac [`E`, `Gamma2`, `v`] \\ fs[])
  >- (qexists_tac `mG` \\ fs[]
      \\ rpt conj_tac
      >- (first_x_assum irule
          \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
      >- (first_x_assum irule
          \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
      >- (first_x_assum irule
          \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
      >- (qexistsl_tac [`m1`, `m2`, `m3`] \\ fs[])
      \\ rpt strip_tac \\ first_x_assum irule
      \\ qexistsl_tac [`E`, `Gamma2`, `v`] \\ fs[])
  \\ rpt conj_tac
  >- (first_x_assum irule
      \\ qexists_tac `map1` \\ simp[GSYM validTypes_def])
  >- (qexists_tac `m1` \\ fs[])
  \\ rpt strip_tac \\ first_x_assum irule
  \\ qexistsl_tac [`E`, `Gamma2`, `v`] \\ fs[]);

val map_find_mono = store_thm (
  "map_find_mono",
  ``! e1 e2 m1 m2 Gamma.
      FloverMapTree_mem e2 Gamma = F /\
      FloverMapTree_find e1 Gamma = SOME m1 ==>
      FloverMapTree_find e1 (FloverMapTree_insert e2 m2 Gamma) = SOME m1``,
  rpt strip_tac \\ fs[map_find_add]
  \\ Cases_on `e1 = e2` \\ fs[FloverMapTree_mem_def]);

val getValidMap_correct = store_thm (
  "getValidMap_correct",
  ``! e Gamma akk res.
      (! e.
        FloverMapTree_mem e akk ==>
        validTypes e akk) /\
      getValidMap Gamma e akk = Succes res ==>
      ! e. FloverMapTree_mem e res ==>
        validTypes e res``,
  Induct_on `e` \\ simp[Once getValidMap_def] \\ rpt strip_tac
  >- (Cases_on `FloverMapTree_mem (Var n) akk`
      \\ fs[] \\ rveq \\ fs[option_case_eq]
      \\ rveq
      \\ fs[FloverMapTree_mem_def]
      \\ Cases_on `FloverMapTree_find e (insert (Var n) m akk)`
      \\ fs[map_find_add]
      \\ Cases_on `e = Var n` \\ fs[]
      >- (rveq \\ simp[Once validTypes_def]
          \\ qexists_tac `m` \\ fs[map_find_add]
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Var n`, `m`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def])
      \\ `validTypes e akk`
        by (first_x_assum irule \\ fs[])
      \\ irule validTypes_mono \\ find_exists_tac \\ fs[]
      \\ rpt strip_tac \\ fs[map_find_add]
      \\ COND_CASES_TAC \\ fs[])
  >- (Cases_on `FloverMapTree_mem (Const m v) akk`
      \\ fs[] \\ rveq \\ fs[]
      \\ Cases_on `isMonotone (FloverMapTree_find (Const m v) Gamma) m`
      \\ fs[] \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
      \\ reverse (Cases_on `e = Const m v`) \\ fs[]
      >- (irule validTypes_mono
          \\ qexists_tac `akk` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      \\ simp[Once validTypes_def]
      \\ fs[map_find_add]
      \\ rpt strip_tac \\ rveq
      \\ first_x_assum (qspecl_then [`Const m v`, `m`] assume_tac)
      \\ fs[eval_expr_cases, toRExpMap_def])
  >- (qpat_x_assum `getValidMap _ _ _ = _`
        (fn thm => assume_tac (ONCE_REWRITE_RULE [getValidMap_def] thm))
      \\ Cases_on `FloverMapTree_mem (Unop u e) akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e akk` \\ fs[]
      \\ Cases_on `FloverMapTree_find e a` \\ fs[]
      \\ `! e. FloverMapTree_mem e a  ==> validTypes e a`
        by (rpt strip_tac \\ first_x_assum irule \\ fs[]
            \\ qexistsl_tac [`Gamma`, `akk`] \\ fs[])
      \\ Cases_on `isFixedPoint x` \\ fs[option_case_eq]
      >- (Cases_on `isCompat x mFix` \\ fs[addMono_def, option_case_eq]
          \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
          \\ reverse (Cases_on `e' = Unop u e`) \\ fs[]
          >- (irule validTypes_mono
              \\ qexists_tac `a` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              >- (COND_CASES_TAC \\ fs[])
              \\ first_x_assum irule \\ fs[])
          \\ rveq \\ once_rewrite_tac[validTypes_def]
          \\ qexists_tac `mFix` \\ fs[map_find_add]
          \\ rpt conj_tac
          >- (irule validTypes_mono
              \\ qexists_tac `a` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (fs[no_cycle_unop])
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Unop u e`, `mFix`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
      \\ Cases_on `isMonotone (FloverMapTree_find (Unop u e) Gamma) x`
      \\ fs[addMono_def, option_case_eq]
      \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
      \\ reverse (Cases_on `e' = Unop u e`) \\ fs[]
      >- (irule validTypes_mono
          \\ qexists_tac `a` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          >- (COND_CASES_TAC \\ fs[])
          \\ first_x_assum irule \\ fs[])
      \\ rveq \\ once_rewrite_tac[validTypes_def]
      \\ qexists_tac `x` \\ fs[map_find_add]
      \\ rpt conj_tac
      >- (irule validTypes_mono
          \\ qexists_tac `a` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (Cases_on `x` \\ fs[isCompat_def, morePrecise_def])
      \\ rpt strip_tac
      \\ first_x_assum (qspecl_then [`Unop u e`, `x`] assume_tac)
      \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
  >- (qpat_x_assum `getValidMap _ _ _ = _`
        (fn thm => assume_tac (ONCE_REWRITE_RULE [getValidMap_def] thm))
      \\ rename1 `Binop b e1 e2`
      \\ Cases_on `FloverMapTree_mem (Binop b e1 e2) akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e1 akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e2 a` \\ fs[]
      \\ rename1 `getValidMap Gamma e2 a = Succes map2`
      \\ Cases_on `FloverMapTree_find e1 map2` \\ fs[]
      \\ Cases_on `FloverMapTree_find e2 map2` \\ fs[]
      \\ `! e. FloverMapTree_mem e a  ==> validTypes e a`
        by (rpt strip_tac \\ last_x_assum irule \\ fs[]
            \\ qexistsl_tac [`Gamma`, `akk`] \\ fs[])
      \\ `! e. FloverMapTree_mem e map2 ==> validTypes e map2`
        by (rpt strip_tac \\ first_x_assum irule \\ fs[]
            \\ qexistsl_tac [`Gamma`, `a`] \\ fs[])
      \\ Cases_on `isFixedPoint x /\ isFixedPoint x'` \\ fs[option_case_eq]
      >- (Cases_on `morePrecise x mj` \\ Cases_on `morePrecise x' mj`
          \\ fs[addMono_def, option_case_eq]
          \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
          \\ reverse (Cases_on `e'' = Binop b e1 e2`) \\ fs[]
          >- (irule validTypes_mono
              \\ qexists_tac `map2` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              >- (COND_CASES_TAC \\ fs[]))
          \\ rveq \\ once_rewrite_tac[validTypes_def]
          \\ qexists_tac `mj` \\ fs[map_find_add]
          \\ rpt conj_tac
          >- (irule validTypes_mono
              \\ qexists_tac `map2` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map2` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (fs[no_cycle_binop_left, no_cycle_binop_right, isJoin_def])
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Binop b e1 e2`, `mj`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
      \\ fs[option_case_eq]
      \\ Cases_on `isMonotone (FloverMapTree_find (Binop b e1 e2) Gamma) m`
      \\ fs[addMono_def, option_case_eq]
      \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
      \\ reverse (Cases_on `e'' = Binop b e1 e2`) \\ fs[]
      >- (irule validTypes_mono
          \\ qexists_tac `map2` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (rveq \\ once_rewrite_tac[validTypes_def]
          \\ qexists_tac `m` \\ fs[map_find_add]
          \\ rpt conj_tac
          >- (irule validTypes_mono
              \\ qexists_tac `map2` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map2` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (fs[no_cycle_binop_left, no_cycle_binop_right, isJoin_def])
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Binop b e1 e2`, `m`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
      >- (irule validTypes_mono
          \\ qexists_tac `map2` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      \\ rveq \\ once_rewrite_tac[validTypes_def]
      \\ qexists_tac `m` \\ fs[map_find_add]
      \\ rpt conj_tac
      >- (irule validTypes_mono
          \\ qexists_tac `map2` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (irule validTypes_mono
          \\ qexists_tac `map2` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (fs[no_cycle_binop_left, no_cycle_binop_right, isJoin_def])
      \\ rpt strip_tac
      \\ first_x_assum (qspecl_then [`Binop b e1 e2`, `m`] assume_tac)
      \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
  >- (qpat_x_assum `getValidMap _ _ _ = _`
        (fn thm => assume_tac (ONCE_REWRITE_RULE [getValidMap_def] thm))
      \\ rename1 `Fma e1 e2 e3` \\ fs[]
      \\ Cases_on `FloverMapTree_mem (Fma e1 e2 e3) akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e1 akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e2 a` \\ fs[]
      \\ rename1 `getValidMap Gamma e2 a = Succes map2`
      \\ Cases_on `getValidMap Gamma e3 map2` \\ fs[]
      \\ rename1 `getValidMap Gamma e3 map2 = Succes map3`
      \\ Cases_on `FloverMapTree_find e1 map3` \\ fs[]
      \\ Cases_on `FloverMapTree_find e2 map3` \\ fs[]
      \\ Cases_on `FloverMapTree_find e3 map3` \\ fs[]
      \\ `! e. FloverMapTree_mem e a  ==> validTypes e a`
        by (rpt strip_tac \\ last_x_assum irule \\ fs[]
            \\ qexistsl_tac [`Gamma`, `akk`] \\ fs[])
      \\ `! e. FloverMapTree_mem e map2 ==> validTypes e map2`
        by (rpt strip_tac
            \\ qpat_x_assum `!Gamma akk res. _ /\ getValidMap _ e3 _ = _ ==> _` kall_tac
            \\ first_x_assum irule \\ fs[]
            \\ qexistsl_tac [`Gamma`, `a`] \\ fs[])
      \\ `! e. FloverMapTree_mem e map3  ==> validTypes e map3`
        by (rpt strip_tac \\ first_x_assum irule \\ fs[]
            \\ qexistsl_tac [`Gamma`, `map2`] \\ fs[])
      \\ Cases_on `isFixedPoint x /\ isFixedPoint x' /\ isFixedPoint x''`
      \\ fs[option_case_eq]
      >- (Cases_on `morePrecise x mj` \\ Cases_on `morePrecise x' mj`
          \\ Cases_on `morePrecise x'' mj`
          \\ fs[addMono_def, option_case_eq]
          \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
          \\ rename1 `if eNew = Fma e1 e2 e3 then SOME mj else _`
          \\ reverse (Cases_on `eNew = Fma e1 e2 e3`) \\ fs[]
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          \\ rveq \\ once_rewrite_tac[validTypes_def]
          \\ qexists_tac `mj` \\ fs[map_find_add]
          \\ rpt conj_tac
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (fs[no_cycle_fma_left, no_cycle_fma_center, no_cycle_fma_right,
                 isJoin3_def])
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Fma e1 e2 e3`, `mj`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
      \\ fs[option_case_eq]
      \\ Cases_on `isMonotone (FloverMapTree_find (Fma e1 e2 e3) Gamma) m`
      \\ fs[addMono_def, option_case_eq]
      \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
      \\ rename1 `eNew = Fma e1 e2 e3`
      \\ reverse (Cases_on `eNew = Fma e1 e2 e3`) \\ fs[]
      >- (irule validTypes_mono
          \\ qexists_tac `map3` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (rveq \\ once_rewrite_tac[validTypes_def]
          \\ qexists_tac `m` \\ fs[map_find_add]
          \\ rpt conj_tac
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (fs[no_cycle_fma_left, no_cycle_fma_center, no_cycle_fma_right,
                 isJoin3_def])
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Fma e1 e2 e3`, `m`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
      >- (irule validTypes_mono
          \\ qexists_tac `map3` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (rveq \\ once_rewrite_tac[validTypes_def]
          \\ qexists_tac `m` \\ fs[map_find_add]
          \\ rpt conj_tac
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (irule validTypes_mono
              \\ qexists_tac `map3` \\ fs[]
              \\ rpt strip_tac \\ fs[map_find_add]
              \\ COND_CASES_TAC \\ fs[])
          >- (fs[no_cycle_fma_left, no_cycle_fma_center, no_cycle_fma_right,
                 isJoin3_def])
          \\ rpt strip_tac
          \\ first_x_assum (qspecl_then [`Fma e1 e2 e3`, `m`] assume_tac)
          \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
      >- (irule validTypes_mono
          \\ qexists_tac `map3` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      \\ rveq \\ once_rewrite_tac[validTypes_def]
      \\ qexists_tac `m` \\ fs[map_find_add]
      \\ rpt conj_tac
      >- (irule validTypes_mono
          \\ qexists_tac `map3` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (irule validTypes_mono
          \\ qexists_tac `map3` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (irule validTypes_mono
          \\ qexists_tac `map3` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (fs[no_cycle_fma_left, no_cycle_fma_center, no_cycle_fma_right,
             isJoin3_def])
      \\ rpt strip_tac
      \\ first_x_assum (qspecl_then [`Fma e1 e2 e3`, `m`] assume_tac)
      \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
  \\ qpat_x_assum `getValidMap _ _ _ = _`
    (fn thm => assume_tac (ONCE_REWRITE_RULE [getValidMap_def] thm))
  \\ Cases_on `FloverMapTree_mem (Downcast m e) akk` \\ fs[]
  \\ Cases_on `getValidMap Gamma e akk` \\ fs[]
  \\ Cases_on `FloverMapTree_find e a` \\ fs[]
  \\ `! e. FloverMapTree_mem e a  ==> validTypes e a`
    by (rpt strip_tac \\ first_x_assum irule \\ fs[]
        \\ qexistsl_tac [`Gamma`, `akk`] \\ fs[])
  \\ Cases_on `isFixedPoint x` \\ fs[option_case_eq]
  >- (Cases_on `m = mFix` \\ Cases_on `morePrecise x mFix`
      \\ fs[addMono_def, option_case_eq]
      \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
      \\ reverse (Cases_on `e' = Downcast m e`) \\ fs[]
      >- (irule validTypes_mono
          \\ qexists_tac `a` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      \\ rveq \\ once_rewrite_tac[validTypes_def]
      \\ qexists_tac `m` \\ fs[map_find_add]
      \\ rpt conj_tac
      >- (irule validTypes_mono
          \\ qexists_tac `a` \\ fs[]
          \\ rpt strip_tac \\ fs[map_find_add]
          \\ COND_CASES_TAC \\ fs[])
      >- (fs[no_cycle_cast, isMorePrecise_morePrecise])
      \\ rpt strip_tac
      \\ first_x_assum (qspecl_then [`Downcast m e`, `mFix`] assume_tac)
      \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[])
  \\ Cases_on `morePrecise x m`
  \\ Cases_on `isMonotone (FloverMapTree_find (Downcast m e) Gamma) m`
  \\ fs[addMono_def, option_case_eq]
  \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]
  \\ reverse (Cases_on `e' = Downcast m e`) \\ fs[]
  >- (irule validTypes_mono
      \\ qexists_tac `a` \\ fs[]
      \\ rpt strip_tac \\ fs[map_find_add]
      \\ COND_CASES_TAC \\ fs[])
  \\ rveq \\ once_rewrite_tac[validTypes_def]
  \\ qexists_tac `m` \\ fs[map_find_add]
  \\ rpt conj_tac
  >- (irule validTypes_mono
      \\ qexists_tac `a` \\ fs[]
      \\ rpt strip_tac \\ fs[map_find_add]
      \\ COND_CASES_TAC \\ fs[])
  >- (fs[no_cycle_cast, isMorePrecise_morePrecise])
  \\ rpt strip_tac
  \\ first_x_assum (qspecl_then [`Downcast m e`, `m`] assume_tac)
  \\ fs[eval_expr_cases, toRExpMap_def] \\ rveq \\ fs[]);

val getValidMap_top_contained = store_thm (
  "getValidMap_top_contained",
  ``! e akk Gamma res.
      getValidMap Gamma e akk = Succes res ==>
      FloverMapTree_mem e res``,
  Induct_on `e` \\ simp[Once getValidMap_def] \\ rpt strip_tac
  >- (EVERY_CASE_TAC \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add])
  >- (EVERY_CASE_TAC \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add])
  >- (Cases_on `getValidMap Gamma e akk` \\ fs[]
      \\ res_tac
      \\ fs[addMono_def]
      \\ EVERY_CASE_TAC \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add])
  >- (Cases_on `FloverMapTree_mem (Binop b e e') akk` \\ fs[] \\ rveq \\ fs[]
      \\ Cases_on `getValidMap Gamma e akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e' a` \\ fs[]
      \\ res_tac \\ fs[addMono_def]
      \\ EVERY_CASE_TAC \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add])
  >- (rename1 `Fma e1 e2 e3`
      \\ Cases_on `FloverMapTree_mem (Fma e1 e2 e3) akk` \\ fs[] \\ rveq \\ fs[]
      \\ Cases_on `getValidMap Gamma e1 akk` \\ fs[]
      \\ Cases_on `getValidMap Gamma e2 a` \\ fs[]
      \\ Cases_on `getValidMap Gamma e3 a'` \\ fs[]
      \\ res_tac \\ fs[addMono_def]
      \\ EVERY_CASE_TAC \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add])
  \\ Cases_on `getValidMap Gamma e akk` \\ fs[]
  \\ res_tac
  \\ fs[addMono_def]
  \\ EVERY_CASE_TAC \\ rveq \\ fs[FloverMapTree_mem_def, map_find_add]);

val getValidMap_top_correct = store_thm (
  "getValidMap_top_correct",
  ``! e akk Gamma res.
      (! e. FloverMapTree_mem e akk ==> validTypes e akk) /\
      getValidMap Gamma e akk = Succes res ==>
      validTypes e res``,
  metis_tac[getValidMap_correct, getValidMap_top_contained]);

val validTypesCmd_def = Define `
  validTypesCmd f Gamma =
    ((case f of
    | Let m x e g =>
      ? mG.
      FloverMapTree_find e Gamma = SOME mG /\
      FloverMapTree_find (Var x) Gamma = SOME m /\
      m = mG /\
      validTypes e Gamma /\
      validTypesCmd g Gamma
    | Ret e => validTypes e Gamma) /\
    ? mT.
      FloverMapTree_find (getRetExp f) Gamma = SOME mT /\
      (! E Gamma2 v m.
        (! e m. FloverMapTree_find e Gamma = SOME m ==>
                FloverMapTree_find e Gamma2 = SOME m) /\
        bstep f E (toRExpMap Gamma2) v m ==>
        m = mT))`;

val validTypesCmd_single = store_thm (
  "validTypesCmd_single",
  ``! f Gamma.
      validTypesCmd f Gamma ==>
      ? mT.
        FloverMapTree_find (getRetExp f) Gamma = SOME mT /\
        ! E Gamma2 v m.
          (! e m. FloverMapTree_find e Gamma = SOME m ==>
                  FloverMapTree_find e Gamma2 = SOME m) /\
          bstep f E (toRExpMap Gamma2) v m ==>
          m = mT``,
  Cases_on `f` \\ simp[Once validTypesCmd_def] \\ rpt strip_tac \\ metis_tac[]);

val getValidMapCmd_def = Define `
  getValidMapCmd Gamma (Let m x e g) akk =
    do
      res_e <- getValidMap Gamma e akk;
      case FloverMapTree_find e res_e of
      | NONE => Fail "No type computed for argument"
      | SOME m_e =>
        if (m = m_e)
        then
          do
            newMap <- addMono (Var x) m res_e;
            getValidMapCmd Gamma g newMap;
          od
        else Fail "Incorrect type for let-bound variable"
    od /\
  getValidMapCmd Gamma (Ret e) akk = getValidMap Gamma e akk`;

val getValidMapCmd_mono = store_thm (
  "getValidMapCmd_mono",
  ``! f Gamma akk res.
      getValidMapCmd Gamma f akk = Succes res ==>
      ! e m. FloverMapTree_find e akk = SOME m ==>
             FloverMapTree_find e res = SOME m``,
  Induct_on `f` \\ simp[Once getValidMapCmd_def]
  THENL [ ALL_TAC , MATCH_ACCEPT_TAC getValidMap_mono]
  \\ rpt strip_tac
  \\ Cases_on `getValidMap Gamma e akk` \\ fs[option_case_eq]
  \\ Cases_on `m = m_e` \\ fs[] \\ rveq
  \\ Cases_on `addMono (Var n) m a` \\ fs[addMono_def, option_case_eq]
  \\ rveq
  \\ res_tac
  \\ first_x_assum irule
  \\ fs[map_find_add]
  \\ COND_CASES_TAC \\ fs[]
  >- (rveq
      \\ `FloverMapTree_find (Var n) a = SOME m'`
        by (irule getValidMap_mono \\ qexistsl_tac [`Gamma`, `akk`, `e`]
            \\ fs[])
      \\ fs[])
  \\ irule getValidMap_mono \\ ntac 2 (find_exists_tac \\ fs[]));

val getValidMapCmd_correct = store_thm (
  "getValidMapCmd_correct",
  ``! f Gamma akk res.
      (! e. FloverMapTree_mem e akk ==>
            validTypes e akk) /\
      getValidMapCmd Gamma f akk = Succes res ==>
      validTypesCmd f res /\
      (! e. FloverMapTree_mem e res ==>
            validTypes e res)``,
  Induct_on `f` \\ simp[getValidMapCmd_def]
  \\ rpt gen_tac \\ rpt (disch_then assume_tac) \\ fs[]
  >- (Cases_on `getValidMap Gamma e akk` \\ fs[option_case_eq]
      \\ Cases_on `m = m_e` \\ fs[] \\ rveq
      \\ Cases_on `addMono (Var n) m a` \\ fs[addMono_def, option_case_eq]
      \\ rveq
      \\ `validTypesCmd f res /\
          ! e. FloverMapTree_mem e res ==> validTypes e res`
        by (first_x_assum irule \\ qexistsl_tac [`Gamma`, `insert (Var n) m a`]
            \\ fs[] \\ rpt strip_tac \\ fs[FloverMapTree_mem_def, map_find_add]
            \\ Cases_on `e' = Var n`
            >- (rveq \\ simp[validTypes_def]
                \\ qexists_tac `m` \\ fs[map_find_add]
                \\ rpt strip_tac
                \\ first_x_assum (qspecl_then [`Var n`, `m`] assume_tac)
                \\ fs[eval_expr_cases, toRExpMap_def])
            \\ fs[] \\ irule validTypes_mono
            \\ qexists_tac `a` \\ conj_tac
            >- (rpt strip_tac \\ irule map_find_mono
                \\ fs[FloverMapTree_mem_def])
            \\ irule getValidMap_correct
            \\ conj_tac \\ fs[FloverMapTree_mem_def]
            \\ qexistsl_tac [`Gamma`, `akk`, `e`] \\ fs[])
      \\ simp[Once validTypesCmd_def, getRetExp_def]
      \\ IMP_RES_TAC validTypesCmd_single
      \\ rpt conj_tac
      >- (irule getValidMapCmd_mono
          \\ qexistsl_tac [`Gamma`, `insert (Var n) m a`, `f`] \\ fs[]
          \\ fs[map_find_add] \\ Cases_on `e = Var n` \\ fs[])
      >- (irule getValidMapCmd_mono
          \\ qexistsl_tac [`Gamma`, `insert (Var n) m a`, `f`] \\ fs[]
          \\ fs[map_find_add])
      >- (`? m. FloverMapTree_find e res = SOME m`
          by (qexists_tac `m` \\ irule getValidMapCmd_mono
              \\ qexistsl_tac [`Gamma`, `insert (Var n) m a`, `f`] \\ fs[]
              \\ fs[map_find_add] \\ COND_CASES_TAC \\ fs[])
          \\ first_x_assum irule \\ fs[FloverMapTree_mem_def])
      \\ qexists_tac `mT` \\ fs[]
      \\ rpt strip_tac \\ fs[bstep_cases]
      \\ first_x_assum irule
      \\ ntac 2 (find_exists_tac \\ fs[]))
  \\ simp[Once validTypesCmd_def]
  \\ IMP_RES_TAC getValidMap_correct
  \\ IMP_RES_TAC getValidMap_top_correct
  \\ rpt conj_tac \\ fs[]
  \\ IMP_RES_TAC validTypes_single
  \\ qexists_tac `mG` \\ fs[getRetExp_def]
  \\ rpt strip_tac \\ fs[bstep_cases]
  \\ first_x_assum irule \\ ntac 2 (find_exists_tac \\ fs[]));

val _ = export_theory();