Theory TypeSafe

section ‹Statement preservation›

theory TypeSafe
  imports TypeSafe_Assign
begin

context typesafe_base
begin

text ‹
  Successful statement execution preserves the state invariant when the initial
  state is type-consistent and the environment is fully initialised. The
  assumptions of locale @{locale typesafe_base} remain explicit prerequisites.
›

text ‹The proof is by induction over the statement semantics @{const stmt}, with one case per
  statement form. Each case shows that the resulting state is again type-safe, that the
  environment stays fully initialised, and that memory only grows (see
  @{const StateInvariant}).
  ▪ ‹SKIP› is immediate; ‹COMP›, ‹ITE› and ‹WHILE› follow from the induction hypotheses.
  ▪ The sixteen forms of ‹ASSIGN› are proved separately in @{text TypeSafe_Assign}.
  ▪ ‹INVOKE›, ‹EXTERNAL› and ‹NEW› run a body in a freshly initialised environment and use
    the initialisation results of @{text TypeSafe_Environment} with the induction hypothesis.
  ▪ ‹TRANSFER› uses the balance lemmas of @{text TypeSafe_Base_Types}.
  ▪ The two forms of ‹BLOCK› use the declaration results of @{text TypeSafe_Decl}.›

theorem TypeSafe_Statements:
  assumes "TypeSafe ev (Accounts st) (Stack st) (Memory st) (Storage st) cd"
    and normal:"stmt smt ev cd st = Normal((),st')"
    and inits:"fullyInitialised ev (Accounts st) (Stack st)"
  shows "StateInvariant ev st st' cd"
  using assms(1,2,3)
proof (induction arbitrary: st' rule:stmt.induct)
  case (1 e cd st)
  have *:"st' = st⦇Gas := (state.Gas st) - costs SKIP e cd st⦈" using 1 skip by simp
  moreover have "Stack st = Stack st'" using * by simp
  moreover have memSame:"Memory st' = Memory st" using * by simp
  moreover have "Storage st' = Storage st" using * by simp
  moreover have "Accounts st' = Accounts st" using * by simp
  moreover have "WrittenMem_between (Memory st) (Memory st') = {}" using calculation unfolding WrittenMem_between_def by simp
  moreover have "ReachableMem e (Stack st') (Memory st') = ReachableMem e (Stack st) (Memory st)" unfolding ReachableMem.simps using calculation by metis

  ultimately show ?case using assms 1 * unfolding StateInvariant_def fullyInitialised_def by auto
next
  case (2 lv ex env cd st)
  then show ?case
  proof (cases rule:assign[OF 2(2)])
    case (1 v t2 g l2 t' g' v')
    from 2 1 show ?thesis by (rule TypeSafe_Assign_1)
  next
    case subcase2:(2 v t2 g locationChanged t' g' v')
    from 2 subcase2 show ?thesis by (rule TypeSafe_Assign_2)
  next
    case (3 v t g l t' g' v')
    from 2 3 show ?thesis by (rule TypeSafe_Assign_3)
  next
    case (4 p x t g l g' p' m t' st'')
    from 2 4 show ?thesis by (rule TypeSafe_Assign_4)
  next
    case (5 p x t g l t' g' p' s)
    from 2 5 show ?thesis by (rule TypeSafe_Assign_5)
  next
    case (6 p x t g l t' g' s)
    from 2 6 show ?thesis by (rule TypeSafe_Assign_6)
  next
    case (7 p x t g l t' g' m m')
    from 2 7 show ?thesis by (rule TypeSafe_Assign_7)
  next
    case (8 p x t g l t' g')
    from 2 8 show ?thesis by (rule TypeSafe_Assign_8)
  next
    case (9 p x t g l t' g' p' s)
    from 2 9 show ?thesis by (rule TypeSafe_Assign_9)
  next
    case (10 p x t g l t' g' s)
    from 2 10 show ?thesis by (rule TypeSafe_Assign_10)
  next
    case (11 p x t g l t' g')
    from 2 11 show ?thesis by (rule TypeSafe_Assign_11)
  next
    case (12 p x t g l t' g' p' m st'')
    from 2 12 show ?thesis by (rule TypeSafe_Assign_12)
  next
    case (13 p x t g l t' g')
    from 2 13 show ?thesis by (rule TypeSafe_Assign_13)
  next
    case (14 p x t g l t' g' s)
    from 2 14 show ?thesis by (rule TypeSafe_Assign_14)
  next
    case (15 p x t g l t' g' m m')
    from 2 15 show ?thesis by (rule TypeSafe_Assign_15)
  next
    case (16 p t g' g l t'' t')
    from 2 16 show ?thesis by (rule TypeSafe_Assign_16)
  qed
next
  case (3 s1 s2 e cd st)
  show ?case
  proof(cases "state.Gas st > costs (COMP s1 s2) e cd st")
    case True
    then have a5:"assert Gas (λst. costs (COMP s1 s2) e cd st < state.Gas st) st = Normal ((), st)" by simp
    then obtain sgas where *:"sgas = st⦇Gas := state.Gas st - costs (COMP s1 s2) e cd st⦈" by simp
    then have a6:"modify (λst. st⦇Gas := state.Gas st - costs (COMP s1 s2) e cd st⦈) st = Normal ((), sgas)" by simp
    moreover have sStackSame:"Stack st = Stack sgas" using * by simp
    moreover have sgasSame:"Memory st = Memory sgas" using * by simp
    moreover have "Storage st = Storage sgas" using * by simp
    moreover have "Accounts st = Accounts sgas" using * by simp
    ultimately have a10:"TypeSafe e (Accounts sgas) (Stack sgas) (Memory sgas) (Storage sgas) cd"
      using 3(3) TypeSafe_def True by simp
    then obtain sts1 where s1:"stmt s1 e cd sgas = Normal ((), sts1)" using 3 * stmt.simps(3) comp by meson
    have fi:"fullyInitialised e (Accounts sgas) (Stack sgas)" using 3(5) unfolding fullyInitialised_def using * by simp
    then have cc0:"StateInvariant e sgas sts1 cd" using 3(1)[OF a5 a6 a10 s1] by blast

    then have ts: "TypeSafe e (Accounts sts1) (Stack sts1) (Memory sts1) (Storage sts1) cd"
      using a10 3(1)[of "()" st "()" sgas sts1] a5 a6 * s1 unfolding StateInvariant_def by simp
    then have "stmt s2 e cd sts1 = Normal ((),st')" using 3(4) * s1 using comp by fastforce
    then have cc2:"StateInvariant e sts1 st' cd" using 3(2)[of "()" st "()" sgas "()" "sts1" st'] a5 a6 * s1 ts fi
      using atype_same by (metis cc0 StateInvariant_def)
    then have cc5:"TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd∧
  (∀locs t. accessTypeStore locs (Memory sts1) = Some t ⟶ accessTypeStore locs (Memory st') = Some t)
∧(∀i loc. i < Toploc (Memory sts1) ∧ LSubPrefL2 loc (ShowLnat i) ⟶ accessStore loc
    (Memory sts1) = None ⟶ accessStore loc (Memory st') = None)"
      unfolding StateInvariant_def by blast
    have cc10:"fullyInitialised e (Accounts st') (Stack st')∧
  (∀locs t. accessTypeStore locs (Memory sts1) = Some t ⟶ accessTypeStore locs (Memory st') = Some t)
∧ (∀i loc. i < Toploc (Memory sts1) ∧ LSubPrefL2 loc (ShowLnat i) ⟶ accessStore loc
    (Memory sts1) = None ⟶ accessStore loc (Memory st') = None)"
      using cc2 unfolding StateInvariant_def by blast
    then have cc12:"TypeSafe e (Accounts sts1) (Stack sts1) (Memory sts1) (Storage sts1) cd ∧
    fullyInitialised e (Accounts sts1) (Stack sts1)∧
    (∀locs t. accessTypeStore locs (Memory sgas) = Some t ⟶ accessTypeStore locs (Memory sts1) = Some t) ∧
(∀i loc. i < Toploc (Memory sgas) ∧ LSubPrefL2 loc (ShowLnat i) ⟶ accessStore loc (Memory
    sgas) = None ⟶ accessStore loc (Memory sts1) = None)"
      using cc0 unfolding StateInvariant_def by simp
    have cc15:"TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd ∧
  fullyInitialised e (Accounts st') (Stack st') ∧
  (∀locs t. accessTypeStore locs (Memory sts1) = Some t ⟶ accessTypeStore locs (Memory st') = Some t)
∧ (∀i loc. i < Toploc (Memory sts1) ∧ LSubPrefL2 loc (ShowLnat i) ⟶ accessStore loc
    (Memory sts1) = None ⟶ accessStore loc (Memory st') = None)"
      using cc2 unfolding StateInvariant_def by blast
    have tlocs:"Toploc (Memory st) ≤ Toploc (Memory sts1) ∧ Toploc (Memory sts1) ≤ Toploc (Memory st')"
      using cc0 cc2 unfolding StateInvariant_def using sgasSame by simp
    then have "(∀i loc. i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i)
                ⟶ accessStore loc (Memory st) = None ⟶ accessStore loc (Memory st') = None)"
      using sgasSame cc15 cc12
      by (metis (no_types, lifting) dual_order.strict_trans1)
    then show ?thesis using cc5 cc10 cc15 cc12 unfolding StateInvariant_def
      using sgasSame
      using StateInvariant_def cc0 cc2 tlocs by auto
  next
    case False
    then have "assert Gas (λst. costs (COMP s1 s2) e cd st < state.Gas st) st = Exception Gas" by simp
    then show ?thesis using 3(4) stmt.simps(3) by simp
  qed
next
  case (4 ex s1 s2 e cd st)
  then show ?case
  proof(cases "state.Gas st> costs (ITE ex s1 s2) e cd st")
    case True
    then have a5:"assert Gas (λst. costs (ITE ex s1 s2) e cd st < state.Gas st) st = Normal ((), st)" by simp
    then obtain sgas where *:"sgas = st⦇Gas := state.Gas st - costs (ITE ex s1 s2) e cd st⦈" by simp
    then have a6:"modify (λst. st⦇Gas := state.Gas st - costs (ITE ex s1 s2) e cd st⦈) st = Normal ((), sgas)" by simp
    moreover have "Stack st = Stack sgas" using * by simp
    moreover have "Memory st = Memory sgas" using * by simp
    moreover have "Storage st = Storage sgas" using * by simp
    moreover have "Accounts st = Accounts sgas" using * by simp
    ultimately have a10:"TypeSafe e (Accounts sgas) (Stack sgas) (Memory sgas) (Storage sgas) cd" using 4(3) True by simp
    then consider (atrue) g where "expr ex e cd sgas (state.Gas sgas) = Normal ((KValue (ShowLbool True), Value TBool),g)"
      |(afalse) g where "expr ex e cd sgas (state.Gas sgas) = Normal ((KValue (ShowLbool False), Value TBool),g)" using * 4(4) stmt.simps(4)
      by (simp split:if_split_asm result.split_asm prod.split_asm stackvalue.split_asm type.split_asm types.split_asm)
    then show ?thesis
    proof (cases)
      case atrue
      then obtain g where a20:"expr ex e cd sgas (state.Gas sgas) = Normal ((KValue (ShowLbool True), Value TBool), g)" by simp
      then obtain s'' where a25:"s'' = sgas⦇Gas:= g⦈" by simp
      moreover have "Stack s'' = Stack sgas" using a25 by simp
      moreover have "Memory s'' = Memory sgas" using a25 by simp
      moreover have "Storage s'' = Storage sgas" using a25 by simp
      moreover have "Accounts s'' = Accounts sgas" using a25 by simp
      ultimately have "TypeSafe e (Accounts s'') (Stack s'') (Memory s'') (Storage s'') cd" using assms 4 True atrue a10 a25 by metis
      moreover have "stmt s1 e cd s'' = Normal((),st')" using * a20 stmt.simps(4) 4(4) a25 by (simp split:if_split_asm result.split_asm prod.split_asm)
      moreover have "fullyInitialised e (Accounts s'') (Stack s'')" using 4(5)
        using ‹Accounts s'' = Accounts sgas› ‹Accounts st = Accounts sgas›
        by (simp add: ‹Stack s'' = Stack sgas› ‹Stack st = Stack sgas›)
      ultimately have "StateInvariant e s'' st' cd" using 4(1)[of "()" st "()" sgas "(KValue (ShowLbool True),
          Value TBool)" s'' "(ShowLbool True)" s'' st'] a5 a6 a20 a25
        unfolding StateInvariant_def by simp
      then show ?thesis unfolding StateInvariant_def
        using ‹Memory s'' = Memory sgas› ‹Memory st = Memory sgas› by simp
    next
      case afalse
      then obtain g where a20:"expr ex e cd sgas (state.Gas sgas) = Normal ((KValue (ShowLbool False), Value TBool), g)" and
        a21: "expr ex e cd sgas (state.Gas sgas) ≠ Normal ((KValue (ShowLbool True), Value TBool), g)" by fastforce
      then obtain s'' where a25:"s'' = sgas⦇Gas:= g⦈" by simp
      moreover have "Stack s'' = Stack sgas" using a25 by simp
      moreover have "Memory s'' = Memory sgas" using a25 by simp
      moreover have "Storage s'' = Storage sgas" using a25 by simp
      moreover have "Accounts s'' = Accounts sgas" using a25 by simp
      ultimately have "TypeSafe e (Accounts s'') (Stack s'') (Memory s'') (Storage s'') cd" using assms 4 True afalse a10 a25 by metis
      moreover have "stmt s2 e cd s'' = Normal((),st')" using * a20 a21 stmt.simps(4)[of ex s1 s2 e cd st ] 4(4) a25 by (simp split:if_split_asm result.split_asm prod.split_asm)
      moreover have "fullyInitialised e (Accounts s'') (Stack s'')" using 4(5) unfolding fullyInitialised_def
        using ‹Accounts s'' = Accounts sgas› ‹Accounts st = Accounts sgas›
        by (simp add: ‹Stack s'' = Stack sgas› ‹Stack st = Stack sgas›)
      moreover have "StateInvariant e s'' st' cd" using 4(2)[of "()" st "()" sgas "(KValue (ShowLbool False),
          Value TBool)" s'' "(ShowLbool False)" s'' st']
          a5 a6 a20 a21 a25 calculation by auto
      ultimately show ?thesis
        unfolding StateInvariant_def
        using ‹Memory s'' = Memory sgas› ‹Memory st = Memory sgas› by auto
    qed
  next
    case False
    then show ?thesis using 4(4) stmt.simps(4) by simp
  qed
next
  case (5 ex s0 e cd st)
  show ?case
  proof(cases rule:while[OF 5(4)])
    case (1 g st'')
    then have a5:"assert Gas (λst. costs (WHILE ex s0) e cd st < state.Gas st) st = Normal ((), st)" by simp
    then obtain sgas where *:"sgas = st⦇Gas := state.Gas st - costs (WHILE ex s0) e cd st⦈" by simp
    then have a6:"modify (λst. st⦇Gas := state.Gas st - costs (WHILE ex s0) e cd st⦈) st = Normal ((), sgas)" by simp

    then have a10:"toState (expr ex e cd) sgas
          = Normal ((KValue (ShowLbool True), Value TBool), (st⦇Gas := g⦈))" using 1 * by auto


    have a20:"TypeSafe e (Accounts (st⦇Gas := g⦈)) (Stack (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) (Storage (st⦇Gas := g⦈)) cd"
      using 5(3) by simp
    have a25:"fullyInitialised e (Accounts (st⦇Gas := g⦈)) (Stack (st⦇Gas := g⦈))" using 5(5) unfolding fullyInitialised_def by simp
    then have a30:"StateInvariant e (st⦇Gas := g⦈) st'' cd"
      using 5(1)[OF a5 a6 a10 _ _ a20 1(3) ] unfolding StateInvariant_def by simp
    have cc0:"StateInvariant e st'' st' cd"
      using 5(2)[OF a5 a6 a10 _ _ 1(3) _ 1(4)] a30 unfolding StateInvariant_def by simp

    then have ts:"TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd ∧
  fullyInitialised e (Accounts st') (Stack st')" using cc0 unfolding StateInvariant_def by blast
    have "Memory st = Memory (st⦇Gas := g⦈) ∧ Stack st = Stack (st⦇Gas := g⦈)" by simp
    then show ?thesis using ts a30 cc0 unfolding StateInvariant_def by fastforce
  next
    case (2 g)
    then have "TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd "
      by (simp add: "5.prems"(1))
    moreover have "fullyInitialised e (Accounts st') (Stack st')" using 2
      using "5.prems"(2,3) atype_same fullyInitialised_def by simp
    moreover have "Memory st = Memory st' ∧ Stack st = Stack st'" using 2 by simp
    ultimately show ?thesis unfolding StateInvariant_def by simp
  qed
next
  case (6 i xe e cd st)
  show ?case
  proof(cases rule:invoke[OF 6(3)])
    case (1 ct fb fp f el cdl kl ml g st'')
    then have a5:"assert Gas (λst. costs (INVOKE i xe) e cd st < state.Gas st) st = Normal ((), st)" by simp
    then obtain sgas where *:"sgas = st⦇Gas := state.Gas st - costs (INVOKE i xe) e cd st⦈" by simp
    then have a6:"modify (λst. st⦇Gas := state.Gas st - costs (INVOKE i xe) e cd st⦈) st = Normal ((), sgas)" by simp
    have a10:"option Err (λ_. ep $$ Contract e) sgas = Normal ((ct, fb), sgas)" using 1(2) by simp
    then have a20:"(case ct $$ i of None ⇒ throw Err | Some (Method (fp, True, f)) ⇒ throw Err
     | Some (Method (fp, False, f)) ⇒ return (fp, f) | Some _ ⇒ throw Err)
     sgas = Normal ((fp, f), sgas)" using 1 by auto


    have memSGas:"Memory sgas = Memory st" using * by auto
    have a25:"applyf Memory sgas = Normal ((Memory sgas), sgas)" by auto

    obtain x where xDef:"x = ffold_init ct (emptyEnv (Address e) (Contract e) (Sender e) (Svalue e)) (fmdom ct)" by simp

    have tsX:"TypeSafe x (Accounts sgas) emptyStore (Memory sgas) (Storage sgas) emptyTypedStore"
      using ffoldInitTypeSafe[of "(Accounts sgas)" "(Storage sgas)" "Svalue e" "(Memory sgas)" _ ct fb "Address e" _ _]
      using * 6(2) "6.prems"(3) unfolding TypeSafe_def fullyInitialised_def using 1(2) xDef ffold_init_def by auto
    have cx:"Contract x = Contract e" using xDef by simp
    then have a27:"load False fp xe x emptyTypedStore emptyStore (Memory sgas) e cd (sgas) (state.Gas st - costs (INVOKE i xe) e cd st) =
    Normal ((el, cdl, kl, ml), g)" using 1(4) * xDef unfolding ffold_init_def by simp
    then have a30:"toState (load False fp xe x emptyTypedStore emptyStore (Memory sgas) e cd) sgas =
  Normal ((el, cdl, kl, ml), sgas⦇Gas := g⦈) " using * 1(5) by auto
    have sameADD:"Address e = Address x"
      by (simp add: xDef)
    have link:"TypeSafe el (Accounts sgas) kl ml (Storage sgas) cdl ∧
    fullyInitialised el (Accounts sgas) kl ∧
    (¬ False ⟶
     (∀locs tp. MCon tp (Memory sgas) locs ⟶ MCon tp ml locs) ∧
     Toploc (Memory sgas) ≤ Toploc ml ∧
     ncpDenvalueLimit el e kl (Stack sgas) (Memory sgas) ∧
     ncpOMemInDMem (Memory sgas) ml ∧ ncpElementsNoSubPref (Memory sgas) ml ∧ ncpNewSelfPoint (Memory sgas) ml)"
    proof -
      have "(∀locs tp. MCon tp (Memory sgas) locs ⟶ MCon tp (Memory sgas) locs)" by blast
      moreover have "Toploc (Memory sgas) ≤ Toploc (Memory sgas)" by simp
      moreover have "ncpDenvalueLimit x e emptyStore (Stack sgas) (Memory sgas)" unfolding ncpDenvalueLimit_def accessStore_def emptyStore_def by simp
      moreover have "ncpOMemInDMem (Memory sgas) (Memory sgas)" unfolding ncpOMemInDMem_def by blast
      moreover have "ncpElementsNoSubPref (Memory sgas) (Memory sgas)" using ncpElementsNoSubPref_sameMem 6(2) * by auto
      moreover have "ncpNewSelfPoint (Memory sgas) (Memory sgas)" unfolding ncpNewSelfPoint_def by auto
      moreover have "TypeSafe x (Accounts sgas) emptyStore (Memory sgas) (Storage sgas) emptyTypedStore"
        using ffoldInitTypeSafe[of "(Accounts sgas)" "(Storage sgas)" "Svalue e" "(Memory sgas)" e ct fb "Address e" _ _]
        using * 6(2) "6.prems"(3) unfolding TypeSafe_def fullyInitialised_def using 1(2) xDef ffold_init_def by auto
      moreover have fiE:"fullyInitialised e (Accounts sgas) (Stack sgas)"
        by (simp add: "*" "6.prems"(3))
      moreover have "fullyInitialised x (Accounts sgas) emptyStore"
      proof -
        have addX:"Address x = Address e" by (simp add: xDef)
        have typeX:"Type (Accounts sgas (Address x)) = Some (atype.Contract (Contract x))"
          using fiE addX cx unfolding fullyInitialised_def by force
        have epX:"ep $$ Contract x = Some (ct, fb)" using "1"(2) cx by simp
        have mapX:"∀id v. (ct $$ id = Some (Var v)) = (Denvalue x $$ id = Some (type.Storage v, Storeloc id))"
          using xDef unfolding ffold_init_def
          using ffoldInit_var_storage_mapping_eq[of ct "Address e" "Contract e" "Sender e" "Svalue e" x]
          by blast
        have locX:"∀id v loc. Denvalue x $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
        proof (intro allI impI)
          fix id v loc
          assume hx:"Denvalue x $$ id = Some (type.Storage v, Storeloc loc)"
          have "(Denvalue x $$ id = Some (type.Storage v, Storeloc loc) ⟶ snd (type.Storage v, Storeloc loc) = Storeloc id)"
            using xDef unfolding ffold_init_def
            using ffoldInitAllLocsStorage[
              of ct "e" "e" "e" "Svalue e" "fmdom ct"
            ] by simp
          then show "id = loc" using hx by simp
        qed
        have ptrX:"∀t l p.
            (type.Storage t, Stackloc l) |∈| fmran (Denvalue x) ∧ accessStore l emptyStore = Some (KStoptr p) ⟶
            (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue x) ∧ CompStoType t' t l' p)"
          by (simp add: accessStore_def emptyStore_def)
        show ?thesis
          unfolding fullyInitialised_def using typeX epX mapX locX ptrX by blast
      qed
      ultimately show ?thesis using exprTypeconInduct(4)[of False fp xe x emptyTypedStore emptyStore "Memory sgas" e cd "sgas" "(state.Gas st - costs (INVOKE i xe) e cd st)"
            el cdl kl ml g] a27 6(2) * sameADD by auto
    qed
    then have a40:" TypeSafe el (Accounts (st⦇Gas := g, Stack := kl, Memory := ml⦈)) (Stack (st⦇Gas := g, Stack := kl, Memory := ml⦈))
     (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) (Storage (st⦇Gas := g, Stack := kl, Memory := ml⦈)) cdl" using * by auto

    have typeMstML:"∀locs t. accessTypeStore locs (Memory st) = Some t ⟶ accessTypeStore locs (ml) = Some t "
    proof intros
      fix locs t
      assume in1:"accessTypeStore locs (Memory st) = Some t"
      then have "∃v. accessStore locs (Memory st) = Some v" using 6(2) unfolding TypeSafe_def SomeValSomeTyp_def by auto
      then have "∃i. i < Toploc (Memory sgas) ∧ LSubPrefL2 locs (ShowLnat i)"
        using 6(2) unfolding TypeSafe_def lessThanTopLocs_def using memSGas by presburger
      then have "accessTypeStore locs (Memory sgas) = accessTypeStore locs ml"
        using link unfolding ncpOMemInDMem_def by blast
      then show "accessTypeStore locs ml = Some t" using in1 memSGas by auto
    qed


    have a44:"(Address el) = (Address e)" using xDef a27
      using emptyEnv_address ffold_init_ad ffold_init_def msel_ssel_expr_load_rexp_gas(4) by presburger
    moreover have a45:"Contract el = Contract e" using xDef a27
      using emptyEnv_members ffold_init_contract ffold_init_def msel_ssel_expr_load_rexp_gas(4) by presburger
    ultimately have cc1:"(∃c. Type (Accounts (st⦇Gas := g, Stack := kl, Memory := ml⦈) (Address
        el)) = Some (atype.Contract c) ∧ Contract el = c)"
      using 6(4) unfolding fullyInitialised_def by simp

    have a60:"∀id x'. Denvalue x $$ id = Some x' ⟶ Denvalue el $$ id = Some x'"
      using load_denval_existing_remain(4)[of False fp xe x emptyTypedStore emptyStore "(Memory sgas)" e cd sgas "(state.Gas st - costs
          (INVOKE i xe) e cd st)" el cdl kl ml g] a27 by blast


    have cc3:"(∀t l p.
        (type.Storage t, Stackloc l) |∈| fmran (Denvalue el) ⟶
        accessStore l (Stack (st⦇Gas := g, Stack := kl, Memory := ml⦈)) = Some (KStoptr p) ⟶
        (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue el) ∧ CompStoType t' t l' p))"
      using fullyInitialised_def link by auto

    have a50:" fullyInitialised el (Accounts (st⦇Gas := g, Stack := kl, Memory := ml⦈)) (Stack
        (st⦇Gas := g, Stack := kl, Memory := ml⦈))" using 6(4)
        cc1 fullyInitialised_def link by auto
    have cc10:"applyf Stack (sgas⦇Gas := g⦈) = Normal ((Stack st), sgas⦇Gas := g⦈)" using * by simp
    have IH1:"TypeSafe el (Accounts st'') (Stack st'') (Memory st'') (Storage st'') cdl ∧ fullyInitialised el (Accounts st'') (Stack st'')
                ∧
    (∀locs t. accessTypeStore locs (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) = Some t ⟶ accessTypeStore locs (Memory st'') = Some t) ∧
(∀locs v. accessStore locs (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) = Some (MPointer v)
    ⟶ (∃v'. accessStore locs (Memory st'') = Some (MPointer v'))) ∧
    (∀locs v. accessStore locs (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) = Some (MValue v)
        ⟶ (∃v'. accessStore locs (Memory st'') = Some (MValue v'))) ∧
(∀i loc.
        i < Toploc (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) ∧ LSubPrefL2 loc (ShowLnat i) ⟶
        accessStore loc (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) = None ⟶ accessStore loc (Memory st'') = None) ∧
Toploc (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) ≤ Toploc (Memory st'')"
      using 6(1)[OF a5 a6 a10 _ a20 _ xDef a25 a30 _ _ _ cc10 _ a40 1(5) a50] * unfolding StateInvariant_def by simp
    have cc20:"(Memory st') = (Memory st'') ∧ (Accounts st'') = (Accounts st') ∧ (Storage st'') = (Storage st')" using 1(6) by simp
    have stackOver:"(Stack st') = (Stack st)" using 1(6) by auto


    show ?thesis unfolding TypeSafe_def StateInvariant_def
    proof intros
      show " AddressTypes (Accounts st')" using IH1 cc20 unfolding TypeSafe_def by auto
    next
      show "safeContract (Accounts st') (Storage st')" using IH1 cc20 unfolding TypeSafe_def by auto
    next
      show "unique_locations (Denvalue e)" using 6(2) unfolding TypeSafe_def by blast
    next
      show "compPointers (Stack st') (Denvalue e)" using 6(2) unfolding TypeSafe_def using cc20
        using stackOver by argo
    next
      show "balanceTypes (Accounts st')" using IH1 cc20 unfolding TypeSafe_def by auto
    next
      have "envAddressesWellFormed e" using 6(2) unfolding TypeSafe_def by blast
      then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
    next
      show "svalueTypes (Svalue e)" using 6(2) unfolding TypeSafe_def by blast
    next
      show "lessThanTopLocs (Stack st')" using 6(2) 1(6) unfolding TypeSafe_def by simp
    next
      show "lessThanTopLocs cd" using 6(2) unfolding TypeSafe_def by blast
    next
      show "lessThanTopLocs (Memory st')" using IH1 cc20 unfolding TypeSafe_def by auto
    next
      show " typeCompat (Denvalue e) (Stack st') (Memory st') (Storage st' (Address e)) cd"
        unfolding typeCompat_def
      proof intros
        fix tIn lIn
        assume in1:"(tIn, lIn) |∈| fmran (Denvalue e)"
        show "denvalueCon (Stack st') (Memory st') (Storage st' (Address e)) cd tIn lIn"
        proof (split denvalue.split, intros)
          fix x1 assume stackLoc:"lIn = Stackloc x1"
          show "case accessStore x1 (Stack st') of None ⇒ False
              | Some (KValue val) ⇒ (case tIn of Value typ ⇒ typeCon typ val | _ ⇒ False)
              | Some (KCDptr stloc) ⇒ (case tIn of Calldata struct ⇒ MCon struct cd stloc | _ ⇒ False)
              | Some (KMemptr stloc) ⇒ (case tIn of type.Memory struct ⇒ MCon struct (Memory st') stloc | _ ⇒ False)
              | Some (KStoptr stloc) ⇒ (case tIn of type.Storage struct ⇒ SCon struct stloc (Storage st' (Address e)) | _ ⇒ False)"
          proof (split option.split, intros)
            assume "accessStore x1 (Stack st') = None"
            then show False using 6(2) unfolding TypeSafe_def typeCompat_def using stackOver in1
              by (metis "6.prems"(1) option.discI stackLoc typeSafeAllStacklocsExist)
          next
            fix x2 assume Some:"accessStore x1 (Stack st') = Some x2"
            then have SomeOld:"accessStore x1 (Stack st) = Some x2" using stackOver by simp
            show "case x2 of KValue val ⇒ (case tIn of Value typ ⇒ typeCon typ val | _ ⇒ False)
          | KCDptr stloc ⇒ (case tIn of Calldata struct ⇒ MCon struct cd stloc | _ ⇒ False)
          | KMemptr stloc ⇒ (case tIn of type.Memory struct ⇒ MCon struct (Memory st') stloc | _ ⇒ False)
          | KStoptr stloc ⇒ (case tIn of type.Storage struct ⇒ SCon struct stloc (Storage st' (Address e)) | _ ⇒ False)"
            proof(split stackvalue.split, intros)
              fix x1 assume KValue:"x2 = KValue x1"
              then show "case tIn of Value typ ⇒ typeCon typ x1 | _ ⇒ False"
                using 6(2) unfolding TypeSafe_def typeCompat_def using stackLoc stackOver in1 Some SomeOld by force
            next
              fix x2a assume KCDPtr:"x2 = KCDptr x2a"
              then show "case tIn of Calldata struct ⇒ MCon struct cd x2a | _ ⇒ False"
                using 6(2) unfolding TypeSafe_def typeCompat_def using stackLoc stackOver in1 Some SomeOld by force
            next
              fix x3 assume KMemPtr:"x2 = KMemptr x3"
              then obtain struct where structDef:"tIn = type.Memory struct" using 6(2) unfolding TypeSafe_def typeCompat_def using in1 stackOver Some stackLoc by (cases tIn; fastforce)
              then have McOld:"MCon struct (Memory st) x3" using 6(2) unfolding TypeSafe_def typeCompat_def using in1 stackOver Some stackLoc KMemPtr by (cases tIn; fastforce)
              then have "∃x i. accessStore x3 (Memory st) = Some x ∨ accessStore (hash x3 (ShowLnat i)) (Memory st) = Some x" using MCon_imps_Some by blast
              moreover have "lessThanTopLocs (Memory st)"using 6(2) unfolding TypeSafe_def by auto
              ultimately obtain tloc where tlocDef: "(tloc<Toploc (Memory st) ∧ LSubPrefL2 x3 (ShowLnat tloc))"
                by (meson lessThanTopLocs_def MemLSubPrefL2_specific_imps_general)

              have "(∀locs tp.
      accessTypeStore locs (Memory st) = Some tp ⟶
      (case accessStore locs (Memory st) of None ⇒ False
       | Some (MValue v) ⇒ ∃val. MCon tp (Memory st) locs ∧ tp = MTValue val ∧ accessTypeStore locs (Memory st) = Some tp
       | Some (MPointer p) ⇒ ∃len arr. MCon tp (Memory st) p ∧ tp = MTArray len arr
∧ (∀i<len. accessTypeStore (hash p (ShowLnat i)) (Memory st) = Some arr)))"
                using 6(2) unfolding TypeSafe_def subPrefixStructuralConsistency_def using McOld by auto
              have IH1Link:"(∀locs tp.
       accessTypeStore locs (Memory st') = Some tp ⟶
       (case accessStore locs (Memory st') of None ⇒ False
        | Some (MValue v) ⇒ ∃val. MCon tp (Memory st') locs ∧ tp = MTValue val ∧ accessTypeStore locs (Memory st') = Some tp
        | Some (MPointer p) ⇒ ∃len arr. MCon tp (Memory st') p ∧ tp = MTArray len arr
∧ (∀i<len. accessTypeStore (hash p (ShowLnat i)) (Memory st') = Some arr)))"
                using IH1 cc20 unfolding TypeSafe_def subPrefixStructuralConsistency_def by presburger
              have mlFinalLink:"(∀locs t. accessTypeStore locs (ml) = Some t ⟶ accessTypeStore locs (Memory st') = Some t)"
                using IH1 cc20 by simp
              have linkS:"(∀i loc. i < Toploc (Memory sgas) ∧ LSubPrefL2 loc (ShowLnat i) ⟶
                      accessStore loc (Memory sgas) = accessStore loc ml
                    ∧ accessTypeStore loc (Memory sgas) = accessTypeStore loc ml)"
                using link unfolding ncpOMemInDMem_def by blast
              then have oldLoadedLink:"accessStore x3 (Memory st) = accessStore x3 ml
                    ∧ accessTypeStore x3 (Memory st) = accessTypeStore x3 ml" using tlocDef memSGas by simp
              have SameVAccess:"(∀locs v. accessStore locs (ml) = Some (MPointer v) ⟶ (∃v'. accessStore locs (Memory st') = Some (MPointer v'))) ∧
  (∀locs v. accessStore locs ((ml)) = Some (MValue v) ⟶ (∃v'. accessStore locs (Memory st') = Some (MValue v')))" using IH1 cc20 by auto
              have "MCon struct (Memory st') x3 "
              proof(cases struct)
                case (MTArray x11 x12)
                then have accTypesOld:"∀i<x11. accessTypeStore (hash x3 (ShowLnat i)) (Memory st) = Some x12"
                  using 6(2) unfolding TypeSafe_def denvalueTypeCorrectness_def
                  using in1 Some stackOver KMemPtr structDef stackLoc by fastforce
                then have oldVals:"(∀i<x11.
                                 case accessStore (hash x3 (ShowLnat i)) (Memory st) of None ⇒ False
                                   | Some (MValue val) ⇒ (case x12 of MTArray l a ⇒ False | MTValue typ
                                       ⇒ MCon x12 (Memory st) (hash x3 (ShowLnat i)))
                                   | Some (MPointer loc2) ⇒ (case x12 of MTArray len' arr' ⇒ MCon x12 (Memory st) loc2 | MTValue val ⇒ False)) ∧
                                   (∃p. accessStore x3 (Memory st) = Some (MPointer p) ∨ accessStore x3 (Memory st) = None)"
                  using McOld MCon.simps(2)[of x11 x12 "Memory st" x3] MTArray by simp
                have "∀i<x11. case accessStore (hash x3 (ShowLnat i)) (Memory st') of None ⇒ False
                             | Some (MValue val) ⇒ (case x12 of MTArray l a ⇒ False | MTValue typ ⇒ MCon
                                 x12 (Memory st') (hash x3 (ShowLnat i)))
                             | Some (MPointer loc2) ⇒ (case x12 of MTArray len' arr' ⇒ MCon x12 (Memory st') loc2 | MTValue val ⇒ False)"
                proof intros
                  fix i
                  assume in1:"i<x11"

                  then consider (ptr) p x11' x12' where "accessStore (hash x3 (ShowLnat i)) (Memory st) = Some (MPointer p)
                                                  ∧ x12 = MTArray x11' x12'"
                    | (val) v vt where "accessStore (hash x3 (ShowLnat i)) (Memory st) = Some (MValue v)
                                    ∧ x12 = MTValue vt" using oldVals
                    by (metis MCon.simps(2) MTArray McOld mcon_accessStore)
                  then show "case accessStore (hash x3 (ShowLnat i)) (Memory st') of None ⇒ False
                        | Some (MValue val) ⇒ (case x12 of MTArray l a ⇒ False | MTValue typ ⇒ MCon x12 (Memory st') (hash x3 (ShowLnat i)))
                        | Some (MPointer loc2) ⇒ (case x12 of MTArray len' arr' ⇒ MCon x12 (Memory st') loc2 | MTValue val ⇒ False) "
                  proof(cases)
                    case ptr
                    then obtain t where accNew:"accessStore (hash x3 (ShowLnat i)) (Memory st') = Some (MPointer t)"
                      using linkS tlocDef memSGas SameVAccess
                      by (metis LSubPrefL2_def Not_Sub_More_Specific)
                    have "accessTypeStore (hash x3 (ShowLnat i)) (Memory st) = Some (MTArray x11' x12')"
                      using accTypesOld ptr in1 by simp
                    then have "accessTypeStore (hash x3 (ShowLnat i)) (Memory st') = Some (MTArray x11' x12')"
                      using mlFinalLink linkS tlocDef memSGas Not_Sub_More_Specific LSubPrefL2_def by metis
                    then have "MCon (MTArray x11' x12') (Memory st') t" using IH1Link accNew by fastforce
                    then show ?thesis using accNew ptr MTArray by simp
                  next
                    case val
                    then have accNew:"∃t. accessStore (hash x3 (ShowLnat i)) (Memory st') = Some (MValue t)"
                      using linkS tlocDef memSGas SameVAccess
                      by (metis LSubPrefL2_def Not_Sub_More_Specific)
                    have "accessTypeStore (hash x3 (ShowLnat i)) (Memory st) = Some (MTValue vt)"
                      using accTypesOld val in1 by simp
                    then have "accessTypeStore (hash x3 (ShowLnat i)) (Memory st') = Some (MTValue vt)"
                      using mlFinalLink linkS tlocDef memSGas Not_Sub_More_Specific LSubPrefL2_def by metis
                    then have "MCon (MTValue vt) (Memory st') (hash x3 (ShowLnat i))" using IH1Link accNew by fastforce
                    then show ?thesis using MTArray accNew val by auto
                  qed

                qed
                moreover have "(∃p. accessStore x3 (Memory st') = Some (MPointer p) ∨ accessStore x3 (Memory st') = None)"
                proof -
                  have "∀i loc. i < Toploc ml ∧ LSubPrefL2 loc (ShowLnat i) ⟶
                      accessStore loc ml = None ⟶ accessStore loc (Memory st') = None" using IH1 cc20 by auto
                  then show ?thesis using oldVals oldLoadedLink SameVAccess tlocDef
                    by (metis dual_order.strict_trans1 link memSGas)
                qed
                ultimately show ?thesis using MTArray McOld by auto
              next
                case (MTValue x2'')
                then have "(case accessStore x3 (Memory st) of None ⇒ False | Some (MValue t) ⇒ typeCon x2'' t | Some (MPointer t) ⇒ False)"
                  using McOld by simp
                then obtain t where accOld:"accessStore x3 (Memory st) = Some (MValue t)" by (auto split:option.splits memoryvalue.splits)
                then have accNew:"∃t. accessStore x3 (Memory st') = Some (MValue t)" using oldLoadedLink SameVAccess by auto
                have "accessTypeStore x3 (Memory st) = Some (MTValue x2'')"
                  using 6(2) unfolding TypeSafe_def denvalueTypeCorrectness_def using in1 Some stackOver KMemPtr structDef MTValue
                  using stackLoc by force
                then have "accessTypeStore x3 (Memory st') = Some (MTValue x2'')" using mlFinalLink oldLoadedLink by auto
                then have "MCon (MTValue x2'') (Memory st') x3" using IH1Link accNew by force
                then show ?thesis using MTValue by simp
              qed


              then show "case tIn of type.Memory struct ⇒ MCon struct (Memory st') x3 | _ ⇒ False" using structDef by simp
            next
              fix x4 assume KStoPtr:"x2 = KStoptr x4"

              then have accO:"accessStore x1 (Stack st) = Some( KStoptr x4)"
                using Some 1 by auto
              then obtain struct where structDef: "tIn = type.Storage struct"
                using 6(2) unfolding TypeSafe_def typeCompat_def using Some KStoPtr stackLoc in1 Some SomeOld
                by (cases tIn) fastforce+
              obtain tprnt lprnt where tprntDef: "((type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e) ∧ CompStoType tprnt struct lprnt x4)"
                using 6(4) using accO in1 unfolding fullyInitialised_def
                using stackLoc structDef by blast

              then have "SCon tprnt lprnt (Storage st'' (Address e))"
                using IH1 unfolding StateInvariant_def TypeSafe_def safeContract_def
                using cx a44 a45
                by (metis (mono_tags, lifting) "6.prems"(3) fi_denvalue_storeloc_to_contract_var fullyInitialised_def)

              then have "SCon struct x4 (Storage st'' (Address e))"
                using tprntDef SCon_imps_sublocs by blast
              then show "case tIn of type.Storage struct ⇒ SCon struct x4 (Storage st' (Address e)) | _ ⇒ False "
                using IH1 cc20 stackLoc stackOver in1 Some SomeOld structDef by simp
            qed
          qed
        next
          fix x2 assume storeLoc:"lIn = Storeloc x2"
          then obtain struct where structDef: "tIn = type.Storage struct"
            using 6(2) unfolding TypeSafe_def typeCompat_def using in1
            by (cases tIn) fastforce+
          obtain tprnt lprnt where tprntDef: "((type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e) ∧ CompStoType tprnt struct lprnt x2)"
            using 6(4) using in1 unfolding fullyInitialised_def
            using structDef
            using CompStoType_sameLocNdTyp storeLoc by auto

          then have "SCon tprnt lprnt (Storage st'' (Address e))"
            using IH1 unfolding StateInvariant_def TypeSafe_def safeContract_def
            using cx a44 a45
            by (metis (mono_tags, lifting) "6.prems"(3) fi_denvalue_storeloc_to_contract_var fullyInitialised_def)


          then have "SCon struct x2 (Storage st'' (Address e))"
            using tprntDef SCon_imps_sublocs by blast
          then show "case tIn of type.Storage struct ⇒ SCon struct x2 (Storage st' (Address e)) | _ ⇒ False "
            using IH1 cc20 storeLoc stackOver in1 structDef by simp
        qed
      qed
    next
      show "fullyInitialised e (Accounts st') (Stack st') " using 6(4) 1(6) cc20 unfolding fullyInitialised_def
        using IH1 ‹Address el = Address e› a45 fullyInitialised_def by auto
    next
      show "denvalueTypeCorrectness e (Stack st') (Memory st')" unfolding denvalueTypeCorrectness_def
      proof intros
        fix t l ptr_loc sub_loc
        assume in1:"(type.Memory t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KMemptr ptr_loc)"
        then have "accessStore l (Stack st) = Some (KMemptr ptr_loc)" using 1 by simp

        have "(∀tp' locs p i.
       (type.Memory tp', Stackloc locs) |∈| fmran (Denvalue el) ∧ accessStore locs kl = Some (KMemptr p) ∧ i <
           Toploc (Memory sgas) ∧ LSubPrefL2 p (ShowLnat i) ⟶
       (∃tp'' loc2 p'.
           (type.Memory tp'', Stackloc loc2) |∈| fmran (Denvalue e) ∧
           accessStore loc2 (Stack sgas) = Some (KMemptr p') ∧
           (p' = p ∧ tp'' = tp' ∨ (∃len arr. p' ≠ p ∧ tp'' = MTArray len arr ∧ CompMemType (Memory sgas) len arr tp' p' p))))"
          using link unfolding ncpDenvalueLimit_def by blast
        have "(∀t l ptr_loc sub_loc.
       (type.Memory t, Stackloc l) |∈| fmran (Denvalue el) ∧ accessStore l (Stack st'') = Some (KMemptr ptr_loc) ⟶
       (case t of
        MTArray len arr ⇒
          (∀i<len. accessTypeStore (hash ptr_loc (ShowLnat i)) (Memory st'') = Some arr)
        | MTValue val ⇒ accessTypeStore ptr_loc (Memory st'') = Some (MTValue val)))"
          using IH1 unfolding TypeSafe_def denvalueTypeCorrectness_def by blast
        have "(case t of MTArray len arr ⇒ ∀i<len. accessTypeStore (hash ptr_loc (ShowLnat i)) (Memory st) = Some arr
       | MTValue val ⇒ accessTypeStore ptr_loc (Memory st) = Some (MTValue val))"
          using 6(2) in1 1 unfolding TypeSafe_def denvalueTypeCorrectness_def by simp
        then show "case t of
       MTArray len arr ⇒
         (∀i<len. accessTypeStore (hash ptr_loc (ShowLnat i)) (Memory st') = Some arr)
       | MTValue val ⇒ accessTypeStore ptr_loc (Memory st') = Some (MTValue val)"
          using IH1 memSGas typeMstML cc20 by (cases t; simp)
      qed
    next
      show "SomeValSomeTyp (Memory st')"
        using IH1 TypeSafe_def cc20 by auto
    next
      show "subPrefixStructuralConsistency (Memory st')" using IH1 TypeSafe_def cc20 by simp
    next
      show "⋀locs t. accessTypeStore locs (Memory st) = Some t ⟹ accessTypeStore locs (Memory st') = Some t "
        using IH1 cc20 typeMstML memSGas by auto
    next
      fix locs v
      assume in1:"accessStore locs (Memory st) = Some (MPointer v)"
      then have "∃i. i < Toploc (Memory sgas) ∧ LSubPrefL2 locs (ShowLnat i)"
        using memSGas 6(2) unfolding TypeSafe_def lessThanTopLocs_def by auto
      then have "accessStore locs (ml) = Some (MPointer v)"
        using in1 link memSGas unfolding ncpOMemInDMem_def by auto
      then show " ∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using IH1 cc20 by simp
    next
      fix locs v
      assume in1:"accessStore locs (Memory st) = Some (MValue v)"
      then have "∃i. i < Toploc (Memory sgas) ∧ LSubPrefL2 locs (ShowLnat i)"
        using memSGas 6(2) unfolding TypeSafe_def lessThanTopLocs_def by auto
      then have "accessStore locs (ml) = Some (MValue v)"
        using in1 link memSGas unfolding ncpOMemInDMem_def by auto
      then show " ∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using IH1 cc20 by simp
    next
      fix i loc
      assume in1:"i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) "
        and in2:"accessStore loc (Memory st) = None"
      then have "accessStore loc (ml) = None"
        using in1 link memSGas unfolding ncpOMemInDMem_def by auto
      moreover have "i < Toploc (Memory (st⦇Gas := g, Stack := kl, Memory := ml⦈)) ∧ LSubPrefL2 loc (ShowLnat i)"
        using in1 link memSGas by simp
      ultimately show "accessStore loc (Memory st') = None"
        using IH1 cc20 by auto
    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')" using memSGas link IH1 cc20 by simp
    qed
  qed
next
  case (7 ad i xe val e cd st)
  show ?case
  proof(cases rule:external[OF 7(4)])
    case (1 adv c g ct cn fb' v t g' v' fp f el cdl kl ml g'' acc st'')
    then obtain cc :: environment where ccDef: "c = Contract cc"
      by (metis Environment.select_convs(2))

    then have a5:"assert Gas (λst. costs (EXTERNAL ad i xe val) e cd st < state.Gas st) st = Normal ((), st)" using 1 by simp
    then obtain sgas where *:"sgas = st⦇Gas := state.Gas st - costs (EXTERNAL ad i xe val) e cd st⦈" by simp
    then have a6:"modify (λst. st⦇Gas := state.Gas st - costs (EXTERNAL ad i xe val) e cd st⦈) st = Normal ((), sgas)" by simp
    obtain expGas where a10:" toState (local.expr ad e cd) sgas = Normal ((KValue adv, Value TAddr), expGas)" using 1(2) a6 * by simp
    then have a20:"(case (KValue adv, Value TAddr) of (KValue adv, Value TAddr) ⇒ return adv | (KValue adv, Value _) ⇒ throw Err
                    | (KValue adv, _) ⇒ throw Err | (_, b) ⇒ throw Err) expGas =
    Normal (adv, expGas)" by simp
    then obtain vd where a30:"assert Err (λ_. adv ≠ Address e) expGas = Normal (vd, expGas)" using 1 by simp
    then have a40:"(case Type (Accounts expGas adv) of None ⇒ throw Err expGas
                | Some EOA ⇒ throw Err expGas
                | Some (atype.Contract c) ⇒ return c expGas) = Normal (c, expGas)" using 1 a10 * by auto
    then have a50:"option Err (λ_. ep $$ c) expGas = Normal ((ct, cn, fb'), expGas)" using 1 by simp
    then have a60:"toState (local.expr val e cd) expGas = Normal ((KValue v, Value t), expGas⦇Gas := g'⦈)" using 1 a10 * by auto
    then have a70:"(case (KValue v, Value t) of (KValue v, Value t) ⇒ return (v, t)
                    | (KValue v, _) ⇒ throw Err
                    | (_, b) ⇒ throw Err) (expGas⦇Gas := g'⦈) = Normal ((v,t), expGas⦇Gas := g'⦈)" using 1 a10 * by simp
    then have a80:"option Err (λ_. convert t (TUInt b256) v) (expGas⦇Gas := g'⦈) = Normal (v', expGas⦇Gas := g'⦈)" using 1 by simp
    then obtain loaded where a90:"loaded = ffold_init ct (emptyEnv adv c (Address e) v') (fmdom ct)" using 1(9) by blast
    then have a100:"toState (local.load True fp xe loaded emptyTypedStore emptyStore emptyTypedStore e cd) (expGas⦇Gas := g'⦈)
        = Normal ((el, cdl, kl, ml), expGas⦇Gas := g''⦈)"
      using 1 * a10 by auto
    then have a110:"option Err (λst. transfer (Address e) adv v' (Accounts st)) (expGas⦇Gas := g''⦈) = Normal (acc, expGas⦇Gas := g''⦈)"
      using 1 * a10 by auto
    then have a120:"modify (λst. st⦇Accounts := acc, Stack := kl, Memory := ml⦈) (expGas⦇Gas :=
        g''⦈) = Normal ((), (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈))"
      using 1 * a10 by auto

    have contractLoaded:"Contract el = c" using a90 a100
      using "1"(9) emptyEnv_members ffold_init_contract msel_ssel_expr_load_rexp_gas(4) by presburger
    have addressLoaded: "Address el = Address loaded" using a90 a100
      using "1"(9) ffold_init_def msel_ssel_expr_load_rexp_gas(4) by presburger

    have tsInitAcc:"∀x y. TypeSafe loaded acc emptyStore emptyTypedStore (Storage st) emptyTypedStore ∧
       (Denvalue loaded $$ x = Some y ⟶ snd y = Storeloc x) ∧ (Denvalue loaded $$ x = Some y ⟶
           (∃t1. ct $$ x = Some (Var t1) ∧ fst y = type.Storage t1))"
    proof -
      have sc:"safeContract acc (Storage st)"
        using 7(3) 1(10) transfer_type_same unfolding TypeSafe_def safeContract_def by fastforce
      have svv':"svalueTypes v'" unfolding svalueTypes_def
        using 1(6,7) 7(3) 7(5)
        using exprTypeconInduct(3)[of val e cd "(st⦇Gas := g⦈)" g "KValue v" "Value t" g']
          typeSafeConvert[of t v "(TUInt b256)"] convertSame by auto
      have bt:"balanceTypes acc" using 1(10) unfolding balanceTypes_def
      proof -
        have old:"balanceTypes (Accounts (st⦇Gas := g''⦈))" using 7(3) unfolding TypeSafe_def by simp
        have accExp:"(case subBalance (Address e) v' (Accounts (st⦇Gas := g''⦈)) of None ⇒ None | Some x ⇒ addBalance adv v' x) = Some acc"
          using 1(10) unfolding transfer_def by blast
        have v'TC: "typeCon (TUInt b256) v'"
          using svv' unfolding svalueTypes_def by blast

        show "∀adv'. typeCon (TUInt b256) (Bal (acc adv')) "
        proof
          fix adv'
          show "typeCon (TUInt b256) (Bal (acc adv')) "
          proof(cases "adv' = (Address e)")
            case True
            then have ns:"adv' ≠ adv" using 1 by blast
            then have tco:"typeCon (TUInt b256) (Bal ((Accounts (st⦇Gas := g''⦈)) adv'))"
              using True old accExp balanceTypes_def by blast
            have "Bal (acc (Address e)) = ShowLint (ReadLint (Bal (Accounts (st⦇Gas :=
                g''⦈) (Address e))) - (ReadLint v'))"
              using transfer_subRead[OF 1(10) _ v'TC ] True ns tco by auto
            then show ?thesis using True
              by (metis transfer_Bal_typeCon[unfolded transfer_def, OF accExp] old[unfolded balanceTypes_def])
          next
            case False
            then show ?thesis
            proof(cases "adv' = adv")
              case True
              then have "typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g''⦈) adv))"
                using True old accExp balanceTypes_def by simp
              then have "Bal (acc adv) = (ShowLint ((ReadLint (Bal (Accounts (st⦇Gas := g''⦈) adv))) + (ReadLint v')))"
                using transfer_addRead[OF 1(10)_ v'TC ] 1(3) by blast
              then show ?thesis using True
                using "1"(10,3) Read_ShowL_id ‹typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g''⦈) adv))› checkUInt_def transfer_val1 transfer_val2 by auto
            next
              case f2:False
              then show ?thesis using False transfer_eq[OF 1(10)] old
                using balanceTypes_def by force
            qed
          qed
        qed
      qed

      have ltp:"lessThanTopLocs emptyTypedStore" using typedEmptyTopLocs by simp
      have tp:"ep $$ Contract cc = Some (ct, cn, fb')" using ccDef 1(5) by simp
      have af1:"addressFormat adv" using 7(3) unfolding TypeSafe_def AddressTypes_def using 1 by (simp split:option.splits atype.splits)
      have af2:"addressFormat (Address e)" using 7(3) unfolding TypeSafe_def AddressTypes_def using 1 by simp
      have af3:"AddressTypes acc" using 1(10) transfer_type_same 7(3) unfolding TypeSafe_def unfolding AddressTypes_def by simp
      have tcAdvAcc:"Type (acc adv) = Some (atype.Contract (Contract cc))"
        using 1(10) a10 * a40 ccDef transfer_type_same by (auto split:option.splits atype.splits result.splits)
      have svt:"SomeValSomeTyp emptyTypedStore" unfolding SomeValSomeTyp_def accessStore_def accessTypeStore_def emptyTypedStore_def by auto
      have sp:"subPrefixStructuralConsistency emptyTypedStore" unfolding subPrefixStructuralConsistency_def accessTypeStore_def emptyTypedStore_def by simp
      then have " ffold (init ct) (emptyEnv adv (Contract cc) (Address e) v') (fmdom ct) = loaded" using a90 unfolding ffold_init_def using ccDef by simp
      then show ?thesis
        using ffoldInitTypeSafe[OF sc bt svv' ltp tp af1 af2 af3 sp svt tcAdvAcc,of "(fmdom ct)"] by simp
    qed

    have tsInit:"∀x y. TypeSafe loaded (Accounts st) emptyStore emptyTypedStore (Storage st) emptyTypedStore ∧
       (Denvalue loaded $$ x = Some y ⟶ snd y = Storeloc x) ∧ (Denvalue loaded $$ x = Some y ⟶
           (∃t1. ct $$ x = Some (Var t1) ∧ fst y = type.Storage t1))"
    proof -
      have "safeContract (Accounts st) (Storage st)" using 7(3) unfolding TypeSafe_def AddressTypes_def using 1 by simp
      moreover have svv':"svalueTypes v'" unfolding svalueTypes_def
        using 1(6,7) 7(3) 7(5)
        using exprTypeconInduct(3)[of val e cd "(st⦇Gas := g⦈)" g "KValue v" "Value t" g']
          typeSafeConvert[of t v "(TUInt b256)"] convertSame by auto
      moreover have "balanceTypes (Accounts st)" using 7(3) unfolding TypeSafe_def by simp
      moreover have "lessThanTopLocs emptyStore" using emptyTopLocs by simp
      moreover have "ep $$ Contract cc = Some (ct, cn, fb')" using ccDef 1(5) by simp
      moreover have "addressFormat adv" using 7(3) unfolding TypeSafe_def AddressTypes_def using 1 by (simp split:option.splits atype.splits)
      moreover have "addressFormat (Sender e)" using 7(3) unfolding TypeSafe_def AddressTypes_def using 1 by simp
      moreover have "AddressTypes (Accounts st)" using 7(3) unfolding TypeSafe_def by simp
      moreover have "SomeValSomeTyp emptyTypedStore" unfolding SomeValSomeTyp_def accessTypeStore_def accessStore_def emptyTypedStore_def by simp
      moreover have sp:"subPrefixStructuralConsistency emptyTypedStore" unfolding subPrefixStructuralConsistency_def accessTypeStore_def emptyTypedStore_def by simp
      moreover have "Type (acc adv) = Some (atype.Contract (Contract cc))"
        using 1(10) a10 * a40 ccDef transfer_type_same by (auto split:option.splits atype.splits result.splits)

      ultimately show ?thesis
        using a90 ccDef unfolding ffold_init_def
        using ffoldInitTypeSafe[of acc "Storage st" v' emptyTypedStore cc ct "(cn,fb')" adv _ " (fmdom ct)"]
        using TypeSafe_def tsInitAcc by presburger
    qed

    have contractL:"Contract loaded = c" using a90 by simp
    have addressL:"Address loaded = adv" using a90 by simp
    have fi1:"(∃c. Type (Accounts (st⦇Gas := g'⦈) (Address loaded)) = Some (atype.Contract c) ∧ Contract loaded = c)" using contractL addressL 1 a10 * a40 by simp
    have "∃dud. ep $$ Contract loaded = Some (ct, dud)" using a50 contractL 1 a10 * by blast
    moreover have "∀id v. (ct $$ id = Some (Var v)) = (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id))"
      using a90 unfolding ffold_init_def using ffoldInit_var_storage_mapping_eq[of ct adv c "Address e" v' loaded] by blast


    have "Type (Accounts (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈) (Address el)) =
        Type (Accounts (st⦇Gas := g'⦈) (Address loaded))"
      using addressLoaded 1(10) a10 * transfer_type_same by simp
    then have fi3:"(∃c. Type (Accounts (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈)
        (Address el)) = Some (atype.Contract c) ∧ Contract el = c)"
      using contractLoaded fi1
      using contractL by presburger


    then have tsLoaded:"TypeSafe el (Accounts (st⦇Gas := g'⦈)) kl ml (Storage (st⦇Gas := g'⦈)) cdl ∧
    fullyInitialised el (Accounts (st⦇Gas := g'⦈)) kl"
    proof -
      have ld:"load True fp xe loaded emptyTypedStore emptyStore emptyTypedStore e cd (st⦇Gas := g'⦈) g' = Normal ((el, cdl, kl, ml), g'')" using 1
        using a90 ffold_init_def by presburger

      have "fullyInitialised loaded (Accounts (st⦇Gas := g'⦈)) emptyStore"
      proof -
        obtain c_fi where cfi:
          "Type (Accounts (st⦇Gas := g'⦈) (Address loaded)) = Some (atype.Contract c_fi) ∧ Contract loaded = c_fi"
          using fi1 by blast
        obtain dud_fi where epfi:"ep $$ Contract loaded = Some (ct, dud_fi)"
          using a50 contractL 1 a10 * by blast
        have mapfi:"∀id v. (ct $$ id = Some (Var v)) = (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id))"
          using a90 unfolding ffold_init_def using ffoldInit_var_storage_mapping_eq[of ct adv c "Address e" v' loaded]
          by blast
        have locfi:"∀id v loc. Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
        proof (intro allI impI)
          fix id v loc
          assume h:"Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc)"
          have hloc:"Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc) ⟶ snd (type.Storage v, Storeloc loc) = Storeloc id"
            using tsInit by blast
          then show "id = loc" using h by simp
        qed
        have ptrfi:"∀t l p.
            (type.Storage t, Stackloc l) |∈| fmran (Denvalue loaded) ∧ accessStore l emptyStore = Some (KStoptr p) ⟶
            (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue loaded) ∧ CompStoType t' t l' p)"
          by (simp add: accessStore_def emptyStore_def)
        show ?thesis using cfi epfi mapfi locfi ptrfi
          using fullyInitialised_def by blast
      qed
      then show ?thesis
        using * 1(9) 7(3) tsInit 7(5) using fi1 ld
        using exprTypeconInduct(4)[of True fp xe loaded emptyTypedStore emptyStore emptyTypedStore e cd "(st⦇Gas := g'⦈)" g' el cdl kl ml g'']
        by simp
    qed


    then have ts1:"TypeSafe el (Accounts (expGas⦇Gas :=
        g'', Accounts := acc, Stack := kl, Memory := ml⦈)) (Stack (expGas⦇Gas := g'', Accounts := acc, Stack :=
        kl, Memory := ml⦈))
     (Memory (expGas⦇Gas :=
         g'', Accounts := acc, Stack := kl, Memory := ml⦈)) (Storage (expGas⦇Gas := g'', Accounts := acc, Stack
         := kl, Memory := ml⦈)) cdl"
      using tsInitAcc a10 * 1 unfolding TypeSafe_def by auto

    have fiFinal:"fullyInitialised el (Accounts(expGas⦇Gas :=
        g'', Accounts := acc, Stack := kl, Memory := ml⦈)) (Stack (expGas⦇Gas := g'', Accounts := acc, Stack :=
        kl, Memory := ml⦈))"
    proof -
      from tsLoaded obtain c_fi ct_fi dud_fi where fiOld:"
           Type (Accounts (st⦇Gas := g'⦈) (Address el)) = Some (atype.Contract c_fi) ∧
           Contract el = c_fi ∧
           ep $$ c_fi = Some (ct_fi, dud_fi) ∧
           (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue el $$ id = Some (type.Storage v, Storeloc id))) ∧
           (∀id v loc. Denvalue el $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
           (∀t l p.
               (type.Storage t, Stackloc l) |∈| fmran (Denvalue el) ∧ accessStore l kl = Some (KStoptr p) ⟶
               (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue el) ∧ CompStoType t' t l' p))"
        unfolding fullyInitialised_def by blast
      have typeNew:"Type (Accounts (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈) (Address el))
              = Some (atype.Contract c_fi)"
        using fiOld addressLoaded
          ‹Type (Accounts (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈) (Address
              el)) = Type (Accounts (st⦇Gas := g'⦈) (Address loaded))›
        by simp
      have ptrNew:"∀t l p.
        (type.Storage t, Stackloc l) |∈| fmran (Denvalue el) ∧
        accessStore l (Stack (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈)) = Some (KStoptr p) ⟶
        (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue el) ∧ CompStoType t' t l' p)"
        using fiOld by simp
      show ?thesis
        unfolding fullyInitialised_def using fiOld typeNew ptrNew by blast
    qed

    then have IH1:"StateInvariant el (expGas⦇Gas := g'', Accounts := acc, Stack := kl, Memory := ml⦈) st'' cdl"
      using 7(1)[OF a5 a6 a10 a20 a30 a40 a50 _ _ a60 a70 _ a80 a90 1(8) _ _ _ _ a100 _ _ _ a110 _ _ a120 ts1
          ] 1(11) 1(2) a10 * by fastforce

    have accFinal:"Accounts st' = Accounts st''" using 1 by simp
    have storageFinal:"Storage st' = Storage st''" using 1 by simp

    have SafeC:"safeContract (Accounts st'') (Storage st'')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
    then have "Storage st'' = Storage st'" using 1 by auto
    then have scNew:"∀e ct dud i tp. Type (Accounts
        st'' (Address e)) = Some (atype.Contract (environment.Contract e)) ∧ ep $$ Contract (e::environment) = Some (ct, dud) ∧
            ct $$ i = Some (Var tp) ⟶ SCon tp i (Storage st'' (Address e))"
      using SafeC unfolding safeContract_def by simp
    have acc:"(∀adv. case Type (Accounts st adv) of None ⇒ True | Some EOA ⇒ True | Some (atype.Contract c) ⇒ addressFormat adv ∧ c |∈| fmdom ep)"
      using 7(3) unfolding TypeSafe_def AddressTypes_def by blast
    obtain cOld where "Type (Accounts st (Address e)) = Some (atype.Contract cOld) ∧ Contract e = cOld"
      using 7(5) unfolding fullyInitialised_def by simp
    then obtain ctO dudO where ctoDef: "ep $$ Contract e = Some (ctO, dudO)" using acc
      using "7.prems"(3) typesafe_base.fullyInitialised_def typesafe_base_axioms by blast
    have fi2:"∀id v. (ctO $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))"
      using 7(5) ctoDef unfolding fullyInitialised_def by auto
    have fiLoc:"∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
      using 7(5) unfolding fullyInitialised_def by blast

    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def using 1(12) by simp
      next
        show "safeContract (Accounts st') (Storage st')" using IH1 storageFinal 1 unfolding StateInvariant_def TypeSafe_def by auto
      next
        show "unique_locations (Denvalue e)" using "7.prems"(1) typeSafeUnique by blast
      next
        have old:" compPointers (Stack st'') (Denvalue el)"
          using IH1 unfolding StateInvariant_def TypeSafe_def by blast
        show "compPointers (Stack st') (Denvalue e)" unfolding compPointers_def
        proof intros
          fix tp1 tp2 l1 l2 l1' l2' stl1 stl2
          assume a0:"(type.Storage tp1, l1) |∈| fmran (Denvalue e) ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue e) ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2)"
          then have a5:"accessStore l1' (Stack st') = accessStore l1' (Stack st)" using 1 by simp
          have a10:"accessStore l2' (Stack st') = accessStore l2' (Stack st)" using 1 by auto
          show "if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2
                else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True"
            using 7(3) unfolding TypeSafe_def compPointers_def using a0 a5 a10 by simp
        qed
      next
        show "balanceTypes (Accounts st')" using accFinal IH1 unfolding StateInvariant_def TypeSafe_def by auto
      next
        have "envAddressesWellFormed e" using "7.prems"(1) unfolding TypeSafe_def by simp
        then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
      next
        show "svalueTypes (Svalue e)" using "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using 1(12) "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs cd" using "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using 1(12) "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "typeCompat (Denvalue e) (Stack st') (Memory st') (Storage st' (Address e)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume tc1:"(t, l) |∈| fmran (Denvalue e)"
          show "denvalueCon (Stack st') (Memory st') (Storage st' (Address e)) cd t l"
          proof(cases l)
            case (Stackloc x1)
            then show ?thesis
            proof(cases "accessStore x1 (Stack st')")
              case None
              then show ?thesis using tc1 Stackloc using 7(3) 1(12) unfolding TypeSafe_def typeCompat_def by force
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 7(3) 1(12) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KCDptr x2)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 7(3) 1(12) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KMemptr x3)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 7(3) 1(12) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KStoptr x4)
                then have accO:"accessStore x1 (Stack st) = Some( KStoptr x4)"
                  using Some 1 by auto
                then obtain struct where structDef: "t = type.Storage struct"
                  using 7(3) unfolding TypeSafe_def typeCompat_def using tc1 Some KStoptr Stackloc
                  by (cases t) fastforce+
                obtain tprnt lprnt where tprntDef: "((type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e) ∧ CompStoType tprnt struct lprnt x4)"
                  using 7(5) using accO tc1 unfolding fullyInitialised_def
                  using Stackloc structDef by blast

                then have "SCon tprnt lprnt (Storage st'' (Address e))"
                proof -
                  from tprntDef obtain kpr where kprDef:"Denvalue e $$ kpr = Some (type.Storage tprnt, Storeloc lprnt)"
                    using fmlookup_ran_iff by blast
                  have "kpr = lprnt" using fiLoc kprDef by blast
                  hence denL:"Denvalue e $$ lprnt = Some (type.Storage tprnt, Storeloc lprnt)" using kprDef by simp
                  have "ctO $$ lprnt = Some (Var tprnt)" using fi2 denL by blast
                  moreover have typeEFinal:"Type (Accounts st'' (Address e)) = Some (atype.Contract (Contract e))"
                    using 7(5) atype_same "7.prems"(2,3) unfolding fullyInitialised_def
                    by (metis accFinal)
                  ultimately show ?thesis using scNew ctoDef by blast
                qed
                then have "SCon struct x4 (Storage st'' (Address e))"
                  using tprntDef SCon_imps_sublocs by blast
                then show ?thesis using Stackloc Some KStoptr structDef storageFinal by simp
              qed
            qed
          next
            case (Storeloc x2)
            then obtain struct where structDef: "t = type.Storage struct"
              using 7(3) tc1 unfolding TypeSafe_def typeCompat_def by (cases t,fastforce+)

            then have "SCon struct x2 (Storage st'' (Address e))"
            proof -
              have inRan:"(type.Storage struct, Storeloc x2) |∈| fmran (Denvalue e)"
                using tc1 Storeloc structDef by simp
              then obtain kpr where kprDef:"Denvalue e $$ kpr = Some (type.Storage struct, Storeloc x2)"
                using fmlookup_ran_iff by metis
              have "kpr = x2" using fiLoc kprDef by blast
              hence denX2:"Denvalue e $$ x2 = Some (type.Storage struct, Storeloc x2)" using kprDef by simp
              have "ctO $$ x2 = Some (Var struct)" using fi2 denX2 by blast
              moreover have typeEFinal:"Type (Accounts st'' (Address e)) = Some (atype.Contract (Contract e))"
                using 7(5) atype_same "7.prems"(2,3) unfolding fullyInitialised_def
                by (metis accFinal)
              ultimately show ?thesis using scNew ctoDef by blast
            qed
            then show ?thesis using Storeloc structDef
              by (simp add: storageFinal)
          qed
        qed
      next
        show "denvalueTypeCorrectness e (Stack st') (Memory st')" using 7(3) 1(12) unfolding TypeSafe_def by simp
      next
        show "subPrefixStructuralConsistency (Memory st')" using 7(3) 1(12) unfolding TypeSafe_def by simp
      next
        show "SomeValSomeTyp (Memory st')" using 7(3) 1(12) unfolding TypeSafe_def by simp
      qed
    next
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address e)) = Some (atype.Contract c) ∧
          Contract e = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p))"
        using 7(5) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address e)) = Some (atype.Contract c_fi) ∧
          Contract e = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address e)) = Some (atype.Contract c_fi)"
        using fiC atype_same "7.prems"(2,3) statement_with_gas.atype_same statement_with_gas_axioms by blast
      have ptrNew:"∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        using 1 fiFinal 7(5) unfolding fullyInitialised_def by auto
      show "fullyInitialised e (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC fiPtr ptrNew typeNew by blast
    next
      show "⋀locs t. accessTypeStore locs (Memory st) = Some t ⟹ accessTypeStore locs (Memory st') = Some t"
        using 7(3) 1(12) by simp
    next
      show "⋀locs v.
       accessStore locs (Memory st) = Some (MPointer v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using 1(12) by simp
    next
      show "⋀locs v. accessStore locs (Memory st) = Some (MValue v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using 1(12) by simp
    next
      show "⋀i loc.
       i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) ⟹
       accessStore loc (Memory st) = None ⟹ accessStore loc (Memory st') = None"
        using 1(12) by simp
    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')" using 1(12) by simp
    qed
  next
    case (2 adv c g ct cn fb' v t g' v' acc st'')
    then obtain cc :: environment where ccDef: "c = Contract cc"
      by (metis Environment.select_convs(2))

    then have a5:"assert Gas (λst. costs (EXTERNAL ad i xe val) e cd st < state.Gas st) st = Normal ((), st)" using 2 by simp
    then obtain sgas where *:"sgas = st⦇Gas := state.Gas st - costs (EXTERNAL ad i xe val) e cd st⦈" by simp
    then have a6:"modify (λst. st⦇Gas := state.Gas st - costs (EXTERNAL ad i xe val) e cd st⦈) st = Normal ((), sgas)" by simp
    obtain expGas where a10:" toState (local.expr ad e cd) sgas = Normal ((KValue adv, Value TAddr), expGas)" using 2(2) a6 * by simp
    then have a20:"(case (KValue adv, Value TAddr) of (KValue adv, Value TAddr) ⇒ return adv | (KValue adv, Value _) ⇒ throw Err
                    | (KValue adv, _) ⇒ throw Err | (_, b) ⇒ throw Err) expGas =
    Normal (adv, expGas)" by simp
    then obtain vd where a30:"assert Err (λ_. adv ≠ Address e) expGas = Normal (vd, expGas)" using 2 by simp
    then have a40:"(case Type (Accounts expGas adv) of None ⇒ throw Err expGas
                | Some EOA ⇒ throw Err expGas
                | Some (atype.Contract c) ⇒ return c expGas) = Normal (c, expGas)" using 2 a10 * by auto
    then have a50:"option Err (λ_. ep $$ c) expGas = Normal ((ct, cn, fb'), expGas)" using 2 by simp
    then have a60:"toState (local.expr val e cd) expGas = Normal ((KValue v, Value t), expGas⦇Gas := g'⦈)" using 2 a10 * by auto
    then have a70:"(case (KValue v, Value t) of (KValue v, Value t) ⇒ return (v, t)
                    | (KValue v, _) ⇒ throw Err
                    | (_, b) ⇒ throw Err) (expGas⦇Gas := g'⦈) = Normal ((v,t), expGas⦇Gas := g'⦈)" using 2 a10 * by simp
    then have a80:"option Err (λ_. convert t (TUInt b256) v) (expGas⦇Gas := g'⦈) = Normal (v', expGas⦇Gas := g'⦈)" using 2 by simp
    then obtain loaded where a90:"loaded = ffold_init ct (emptyEnv adv c (Address e) v') (fmdom ct)" using 2(9) by blast

    then have a110:"option Err (λst. transfer (Address e) adv v' (Accounts st)) (expGas⦇Gas := g'⦈) = Normal (acc, expGas⦇Gas := g'⦈)"
      using 2 * a10 by auto


    have tsInitAcc:"∀x y. TypeSafe loaded acc emptyStore emptyTypedStore (Storage st) emptyTypedStore ∧
       (Denvalue loaded $$ x = Some y ⟶ snd y = Storeloc x) ∧ (Denvalue loaded $$ x = Some y ⟶
           (∃t1. ct $$ x = Some (Var t1) ∧ fst y = type.Storage t1))"
    proof -
      have "safeContract (Accounts st) (Storage st)" using 7(3) unfolding TypeSafe_def AddressTypes_def using 2 by simp
      moreover have "safeContract acc (state.Storage st)"
        using "2"(9) calculation safeContract_def transfer_type_same by auto
      moreover have svv':"svalueTypes v'" unfolding svalueTypes_def
        using 2(6,7) 7(3) 7(5)
        using exprTypeconInduct(3)[of val e cd "(st⦇Gas := g⦈)" g "KValue v" "Value t" g']
          typeSafeConvert[of t v "(TUInt b256)"] convertSame by auto
      moreover have "balanceTypes acc" using 2(10) unfolding balanceTypes_def
      proof -
        have old:"balanceTypes (Accounts (st⦇Gas := g'⦈))" using 7(3) unfolding TypeSafe_def by simp
        have accExp:"(case subBalance (Address e) v' (Accounts (st)) of None ⇒ None | Some x ⇒ addBalance adv v' x) = Some acc"
          using 2(9) unfolding transfer_def by blast
        have v'TC: "typeCon (TUInt b256) v'"
          using svv' unfolding svalueTypes_def by blast

        show "∀adv'. typeCon (TUInt b256) (Bal (acc adv')) "
        proof
          fix adv'
          show "typeCon (TUInt b256) (Bal (acc adv')) "
          proof(cases "adv' = (Address e)")
            case True
            then have ns:"adv' ≠ adv" using 2 by blast
            then have tco:"typeCon (TUInt b256) (Bal ((Accounts (st⦇Gas := g'⦈)) adv'))"
              using True old accExp balanceTypes_def by blast
            have "Bal (acc (Address e)) = ShowLint (ReadLint (Bal (Accounts (st⦇Gas := g'⦈) (Address e))) - (ReadLint v'))"
              using transfer_subRead[OF 2(9) _ v'TC ] True ns tco by auto
            then show ?thesis using True

              using "2"(9) Read_ShowL_id checkUInt_def tco transfer_val3 v'TC by fastforce
          next
            case False
            then show ?thesis
            proof(cases "adv' = adv")
              case True
              then have "typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv))"
                using True old accExp balanceTypes_def by simp
              then have "Bal (acc adv) = (ShowLint ((ReadLint (Bal (Accounts (st⦇Gas := g'⦈) adv))) + (ReadLint v')))"
                using transfer_addRead[OF 2(9)_ v'TC ] 2(3) by auto
              then show ?thesis using True
                using "2"(9,3) Read_ShowL_id ‹typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv))› checkUInt_def transfer_val1 transfer_val2 by auto
            next
              case f2:False
              then show ?thesis using False transfer_eq[OF 2(9)] old
                using balanceTypes_def by force
            qed
          qed
        qed
      qed
      moreover have "lessThanTopLocs emptyStore" using emptyTopLocs by simp
      moreover have "ep $$ Contract cc = Some (ct, cn, fb')" using ccDef 2(5) by simp
      moreover have "addressFormat adv" using 7(3) unfolding TypeSafe_def AddressTypes_def using 2 by (simp split:option.splits atype.splits)
      moreover have "addressFormat (Sender e)" using 7(3) unfolding TypeSafe_def AddressTypes_def using 2 by simp
      moreover have "AddressTypes acc" using 2(9) transfer_type_same 7(3) unfolding TypeSafe_def unfolding AddressTypes_def by simp
      moreover have "addressFormat (Address e)" using "7.prems"(1) unfolding TypeSafe_def by simp
      moreover have "SomeValSomeTyp emptyTypedStore" unfolding SomeValSomeTyp_def accessTypeStore_def accessStore_def
        by (simp add: emptyTypedStore_def)
      moreover have sp:"subPrefixStructuralConsistency emptyTypedStore" unfolding subPrefixStructuralConsistency_def accessTypeStore_def emptyTypedStore_def by simp
      moreover have "Type (acc adv) = Some (atype.Contract (Contract cc))"
        using 2(9) a10 * a40 ccDef transfer_type_same by (auto split:option.splits atype.splits result.splits)

      ultimately show ?thesis
        using a90 ccDef unfolding ffold_init_def using ffoldInitTypeSafe[of acc "Storage st" v' emptyTypedStore cc ct "(cn,fb')" adv "(Address e)" " (fmdom ct)"]
        using typedEmptyTopLocs by blast
    qed

    have contractL:"Contract loaded = c" using a90 by simp
    have addressL:"Address loaded = adv" using a90 by simp
    have fi1:"(∃c. Type (Accounts (st⦇Gas := g'⦈) (Address loaded)) = Some (atype.Contract c) ∧ Contract loaded = c)" using contractL addressL 2 a10 * a40 by simp
    have ct1:"∃dud. ep $$ Contract loaded = Some (ct, dud)" using a50 contractL 2 a10 * by blast
    then have fi2:"∀id v. (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id)) = (ct $$ id = Some (Var v))"
      using a90 unfolding ffold_init_def using ffoldInit_var_storage_mapping_eq[of ct adv c _ v' loaded] by auto
    then have fi3:"(∃c. Type ((acc) (Address loaded)) = Some (atype.Contract c) ∧ Contract loaded = c)"
      using fi1 contractL 2(9) transfer_type_same by simp

    have a120:" TypeSafe loaded (acc) (emptyStore) (emptyTypedStore) (Storage st) emptyTypedStore" using tsInitAcc
      by simp
    moreover have "fullyInitialised loaded (Accounts (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory :=
        emptyTypedStore⦈)) (Stack (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈))"
    proof -
      obtain c_fi where cfi0:"Type (acc (Address loaded)) = Some (atype.Contract c_fi) ∧ Contract loaded = c_fi"
        using fi3 by blast
      have cfi:"Type (Accounts (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈) (Address loaded))
            = Some (atype.Contract c_fi) ∧ Contract loaded = c_fi"
        using cfi0 by simp
      obtain dud_fi where epfi:"ep $$ Contract loaded = Some (ct, dud_fi)"
        using ct1 by blast
      have mapfi:"∀id v. (ct $$ id = Some (Var v)) = (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id))"
        using fi2 by blast
      have locfi:"∀id v loc. Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
      proof (intro allI impI)
        fix id v loc
        assume h:"Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc)"
        have hloc:"Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc) ⟶ snd (type.Storage v, Storeloc loc) = Storeloc id"
          using tsInitAcc by blast
        then show "id = loc" using h by simp
      qed
      have ptrfi:"∀t l p.
          (type.Storage t, Stackloc l) |∈| fmran (Denvalue loaded) ∧
          accessStore l (Stack (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈)) = Some (KStoptr p) ⟶
          (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue loaded) ∧ CompStoType t' t l' p)"
        by (simp add: accessStore_def emptyStore_def)
      show ?thesis
        unfolding fullyInitialised_def using cfi epfi mapfi locfi ptrfi by blast
    qed
    ultimately have IH1:"StateInvariant loaded (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈) st'' emptyTypedStore"
      using 7(2)[OF a5 a6 a10 a20 a30 a40 a50 _ _ a60 a70 _ a80 a90 2(8) a110,
          of "(cn, fb')" cn fb' _ _ _ _ _ "(st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈)" st'']
        2(10) 2 * a10 a90 by fastforce

    have accFinal:"Accounts st' = Accounts st''" using 2 by simp
    have storageFinal:"Storage st' = Storage st''" using 2 by simp

    have acc:"(∀adv. case Type (Accounts st adv) of None ⇒ True | Some EOA ⇒ True | Some (atype.Contract c) ⇒ addressFormat adv ∧ c |∈| fmdom ep)"
      using 7(3) unfolding TypeSafe_def AddressTypes_def by blast
    obtain cOld where "Type (Accounts st (Address e)) = Some (atype.Contract cOld) ∧Contract e = cOld"
      using 7(5) unfolding fullyInitialised_def by simp
    then obtain ctO dudO where ctoDef: "ep $$ Contract e = Some (ctO, dudO)" using acc
      using "7.prems"(3) typesafe_base.fullyInitialised_def typesafe_base_axioms by blast
    have SafeC:"safeContract (Accounts st'') (Storage st'')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast

    then have scNew:"∀e ct dud i tp. Type (Accounts
        st'' (Address e)) = Some (atype.Contract (environment.Contract e)) ∧ ep $$ Contract (e::environment) = Some (ct, dud) ∧ ct
            $$ i = Some (Var tp) ⟶ SCon tp i (Storage st'' (Address e))"
      using SafeC unfolding safeContract_def by simp


    have fi4:"(∀id v. ∃dud. ep $$ Contract loaded = Some (ct, dud)
                          ∧ ct $$ id = Some (Var v) = (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id)))"
      using fi1 ct1 fi2
      by presburger
    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def using 2 by simp
      next
        show "safeContract (Accounts st') (Storage st')" using IH1 storageFinal 2 unfolding StateInvariant_def TypeSafe_def by auto
      next
        show "unique_locations (Denvalue e)" using "7.prems"(1) typeSafeUnique by blast
      next
        show "compPointers (Stack st') (Denvalue e)" unfolding compPointers_def
        proof intros
          fix tp1 tp2 l1 l2 l1' l2' stl1 stl2
          assume a0:"(type.Storage tp1, l1) |∈| fmran (Denvalue e) ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue e) ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2)"
          then have a5:"accessStore l1' (Stack st') = accessStore l1' (Stack st)" using 2 by simp
          have a10:"accessStore l2' (Stack st') = accessStore l2' (Stack st)" using 2 by auto
          show "if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2
                else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True"
            using 7(3) unfolding TypeSafe_def compPointers_def using a0 a5 a10 by simp
        qed
      next
        show "balanceTypes (Accounts st')" using accFinal IH1 unfolding StateInvariant_def TypeSafe_def by auto
      next
        have "envAddressesWellFormed e" using "7.prems"(1) unfolding TypeSafe_def by simp
        then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
      next
        show "svalueTypes (Svalue e)" using "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using 2 "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs cd" using "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using 2 "7.prems"(1) unfolding TypeSafe_def by simp
      next
        show "typeCompat (Denvalue e) (Stack st') (Memory st') (Storage st' (Address e)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume tc1:"(t, l) |∈| fmran (Denvalue e)"
          show "denvalueCon (Stack st') (Memory st') (Storage st' (Address e)) cd t l"
          proof(cases l)
            case (Stackloc x1)
            then show ?thesis
            proof(cases "accessStore x1 (Stack st')")
              case None
              then show ?thesis using tc1 Stackloc using 7(3) 2 unfolding TypeSafe_def typeCompat_def by force
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 7(3) 2(11) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KCDptr x2)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 7(3) 2(11) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KMemptr x3)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 7(3) 2(11) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KStoptr x4)
                then have accO:"accessStore x1 (Stack st) = Some( KStoptr x4)"
                  using Some 2 by auto
                then obtain struct where structDef: "t = type.Storage struct"
                  using 7(3) unfolding TypeSafe_def typeCompat_def using tc1 Some KStoptr Stackloc
                  by (cases t) fastforce+
                have tOld: "Type (Accounts st (Address e)) = Some (atype.Contract (environment.Contract e))"
                  using "7.prems"(3) unfolding fullyInitialised_def by auto
                obtain tprnt lprnt where tprntDef: "((type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e) ∧ CompStoType tprnt struct lprnt x4)"
                  using 7(5) using accO tc1 unfolding fullyInitialised_def
                  using Stackloc structDef by blast
                then have "SCon tprnt lprnt (Storage st'' (Address e))"
                  using ctoDef IH1 fi4 ct1 scNew
                  by (metis (no_types, lifting) "7.prems"(2,3) tOld
                      accFinal fi_denvalue_storeloc_to_contract_var atype_same)

                then have "SCon struct x4 (Storage st'' (Address e))"
                  using tprntDef SCon_imps_sublocs by blast
                then show ?thesis using Stackloc Some KStoptr structDef storageFinal by simp
              qed
            qed
          next
            case (Storeloc x2)
            then obtain struct where structDef: "t = type.Storage struct"
              using 7(3) tc1 unfolding TypeSafe_def typeCompat_def by (cases t,fastforce+)

            then have "SCon struct x2 (Storage st'' (Address e))"
              using ctoDef IH1 fi4 ct1 scNew fmlookup_ran_iff fullyInitialised_def "7.prems"(3)
            proof -
              have "Type (Accounts st (Address e)) = Some (atype.Contract (environment.Contract e))"
                using "7.prems"(3) unfolding fullyInitialised_def by auto
              then have "Type (Accounts st'' (Address e)) = Some (atype.Contract (environment.Contract e))"
                using atype_same[OF "7.prems"(2)] accFinal by simp
              moreover have "ctO $$ x2 = Some (Var struct)"
                using fi_denvalue_storeloc_to_contract_var[OF "7.prems"(3) ctoDef] tc1 Storeloc structDef by simp
              ultimately show ?thesis using scNew ctoDef by blast
            qed

            then show ?thesis using Storeloc structDef
              by (simp add: storageFinal)
          qed
        qed
      next
        show "denvalueTypeCorrectness e (Stack st') (Memory st')" using 2(11) 7(3) unfolding TypeSafe_def by simp
      next
        show "subPrefixStructuralConsistency (Memory st')" using 2(11) 7(3) unfolding TypeSafe_def by simp
      next
        show "SomeValSomeTyp (Memory st')" using 2(11) 7(3) unfolding TypeSafe_def by simp
      qed
    next
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address e)) = Some (atype.Contract c) ∧
          Contract e = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p))"
        using 7(5) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address e)) = Some (atype.Contract c_fi) ∧
          Contract e = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address e)) = Some (atype.Contract c_fi)"
        using fiC atype_same "7.prems"(2,3) statement_with_gas.atype_same statement_with_gas_axioms by blast
      have ptrNew:"∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        using 7(5) 2 unfolding fullyInitialised_def by auto
      show "fullyInitialised e (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC fiPtr ptrNew typeNew by blast
    next
      show "⋀locs t. accessTypeStore locs (Memory st) = Some t ⟹ accessTypeStore locs (Memory st') = Some t"
        using 7(3) 2(11) by simp
    next
      show "⋀locs v.
       accessStore locs (Memory st) = Some (MPointer v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using 2(11) by simp
    next
      show "⋀locs v. accessStore locs (Memory st) = Some (MValue v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using 2(11) by simp
    next
      show "⋀i loc.
       i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) ⟹
       accessStore loc (Memory st) = None ⟹ accessStore loc (Memory st') = None"
        using 2(11) by simp
    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')"
        using 2(11) by simp
    qed
  qed
next
  case (8 ad ex e cd st)
  show ?case
  proof(cases rule:transfer[OF 8(3)])
    case (1 v t g adv c g' v' acc ct cn f st'')
    then obtain cc :: environment where ccDef: "Contract cc = c"
      by (meson Environment.select_convs(2))
    then have a1:"assert Gas (λst. costs (TRANSFER ad ex) e cd st < state.Gas st) st = Normal ((), st)" using 1 by simp
    have a10:"modify (λst. st⦇Gas := state.Gas st - costs (TRANSFER ad ex) e cd st⦈) st = Normal ((), st⦇Gas := state.Gas st - costs (TRANSFER ad ex) e cd st⦈)"
      by simp
    have a20:"toState (local.expr ad e cd) (st⦇Gas := state.Gas st - costs (TRANSFER ad ex) e cd st⦈) = Normal ((KValue adv, Value TAddr), st⦇Gas := g⦈)"
      using 1 by simp
    then have a30:"(case (KValue adv, Value TAddr) of (KValue adv, Value TAddr) ⇒ return adv | (KValue adv, Value _)
        ⇒ throw Err | (KValue adv, _) ⇒ throw Err | (_, b) ⇒ throw Err)
     (st⦇Gas := g⦈) = Normal (adv, st⦇Gas := g⦈)" using 1 by simp
    have a40:" toState (local.expr ex e cd) (st⦇Gas := g⦈) = Normal ((KValue v, Value t), st⦇Gas := g'⦈)" using 1 by auto
    have a50:"(case (KValue v, Value t) of (KValue v, Value t) ⇒ return (v, t) | (KValue v, _) ⇒ throw Err | (_, b)
        ⇒ throw Err) (st⦇Gas := g'⦈) = Normal ((v, t), (st⦇Gas := g'⦈))"
      using 1 by simp
    then have a60:"option Err (λ_. convert t (TUInt b256) v) (st⦇Gas := g'⦈) = Normal (v', st⦇Gas := g'⦈)" using 1 by simp
    have a70:"applyf Accounts (st⦇Gas := g'⦈) = Normal (Accounts (st⦇Gas := g⦈), (st⦇Gas := g'⦈))" using 1 by simp
    have a80:"Type (Accounts (st⦇Gas := g⦈) adv) = Some (atype.Contract c)" using 1(5) by blast
    have a90:"option Err (λst. transfer (Address e) adv v' (Accounts st)) (st⦇Gas := g'⦈) = Normal (acc, (st⦇Gas := g'⦈))"
      using 1 by simp
    have a95:"option Err (λ_. ep $$ c) (st⦇Gas := g'⦈) = Normal ((ct, cn, f), (st⦇Gas := g'⦈))" using 1 by auto
    have a100:"modify (λst. st⦇Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈) (st⦇Gas := g'⦈)
                  = Normal ((), st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈)" by simp
    obtain loaded where loadedDef:"loaded = ffold_init ct (emptyEnv adv c (Address e) v') (fmdom ct)" by blast

    have tsLoaded':"∀x y. TypeSafe loaded acc emptyStore emptyTypedStore (Storage st) emptyTypedStore ∧
       (Denvalue loaded $$ x = Some y ⟶ snd y = Storeloc x) ∧ (Denvalue loaded $$ x = Some y ⟶
           (∃t1. ct $$ x = Some (Var t1) ∧ fst y = type.Storage t1))"
    proof -
      have "safeContract (Accounts st) (Storage st)" using 8(2) unfolding TypeSafe_def AddressTypes_def using 1 by simp
      moreover have "safeContract acc (Storage st)"
        using "1"(7) calculation safeContract_def transfer_type_same by force
      moreover have svv':"svalueTypes v'" unfolding svalueTypes_def
        using 1(4,3) 8(2) 8(4)
        using exprTypeconInduct(3)[of ex e cd "(st⦇Gas := g⦈)" g "KValue v" "Value t" g']
          typeSafeConvert[of t v "(TUInt b256)"] convertSame by auto
      moreover have "balanceTypes acc" unfolding balanceTypes_def
      proof -
        have old:"balanceTypes (Accounts (st⦇Gas := g'⦈))" using 8(2) unfolding TypeSafe_def by simp
        have accExp:"(case subBalance (Address e) v' (Accounts (st)) of None ⇒ None | Some x ⇒ addBalance adv v' x) = Some acc"
          using 1(7) unfolding transfer_def by blast
        have v'TC: "typeCon (TUInt b256) v'"
          using svv' unfolding svalueTypes_def by blast

        show "∀adv'. typeCon (TUInt b256) (Bal (acc adv')) "
        proof(cases "adv = (Address e)")
          case same:True
          show ?thesis using transfer_sameRead
            by (metis "1"(7) "8"(2) balanceTypes_def same typeSafeAccounts transfer_eq v'TC)
        next
          case notSame:False
          show ?thesis
          proof
            fix adv'
            show "typeCon (TUInt b256) (Bal (acc adv')) "
            proof(cases "adv' = (Address e)")
              case True
              then have tco:"typeCon (TUInt b256) (Bal ((Accounts (st⦇Gas := g'⦈)) adv'))"
                using True old accExp balanceTypes_def by blast
              have "Bal (acc (Address e)) = ShowLint (ReadLint (Bal (Accounts (st⦇Gas :=
                  g'⦈) (Address e))) - (ReadLint v'))"
                using transfer_subRead[OF 1(7) _ v'TC ] True notSame tco by auto
              then show ?thesis using True
                using "1"(7,3) Read_ShowL_id ‹typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv'))› checkUInt_def transfer_val1 transfer_val2
              proof -
                have "∀a. typeCon (TUInt b256) (Bal (Accounts st a))"
                  using old unfolding balanceTypes_def by simp
                then show ?thesis using transfer_Bal_typeCon[unfolded transfer_def, OF accExp] by blast
              qed
            next
              case False
              then show ?thesis
              proof(cases "adv' = adv")
                case True
                then have "typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv))"
                  using True old accExp balanceTypes_def by simp
                then have "Bal (acc adv) = (ShowLint ((ReadLint (Bal (Accounts (st⦇Gas := g'⦈) adv))) + (ReadLint v')))"
                  using transfer_addRead[OF 1(7)_ v'TC ] 1(3) notSame by auto
                then show ?thesis using True
                  using "1"(7,3) Read_ShowL_id ‹typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv))› checkUInt_def transfer_val1 transfer_val2 notSame by auto
              next
                case f2:False
                then show ?thesis using False transfer_eq[OF 1(7)] old
                  using balanceTypes_def by force
              qed
            qed
          qed
        qed
      qed
      moreover have "lessThanTopLocs emptyStore" using emptyTopLocs by simp
      moreover have "ep $$ Contract cc = Some (ct, cn, f)" using ccDef 1(6) by simp
      moreover have "addressFormat adv" using 8(2) unfolding TypeSafe_def AddressTypes_def using 1 by (simp split:option.splits atype.splits)
      moreover have "addressFormat (Sender e)" using 8(2) unfolding TypeSafe_def AddressTypes_def using 1 by simp
      moreover have "AddressTypes acc" using 1(7) transfer_type_same 8(2) unfolding TypeSafe_def unfolding AddressTypes_def by simp
      moreover have "addressFormat (Address e)" using "8.prems"(1) unfolding TypeSafe_def by simp
      moreover have "SomeValSomeTyp emptyTypedStore" unfolding SomeValSomeTyp_def accessTypeStore_def accessStore_def emptyTypedStore_def by auto
      moreover have sp:"subPrefixStructuralConsistency emptyTypedStore" unfolding subPrefixStructuralConsistency_def accessTypeStore_def emptyTypedStore_def by simp
      moreover have "Type (acc adv) = Some (atype.Contract (Contract cc))"
        using a80 ccDef transfer_type_same[OF 1(7)] by simp
      ultimately show ?thesis
        using loadedDef ccDef unfolding ffold_init_def
        using ffoldInitTypeSafe[of acc "Storage st" v' emptyTypedStore cc ct "(cn,f)" adv "(Address e)" " (fmdom ct)"]
        using typedEmptyTopLocs
        by blast
    qed
    then have tsLoaded:"TypeSafe loaded (Accounts (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈))
     (Stack (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈)) (Memory (st⦇Gas := g',
         Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈))
     (Storage (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈)) emptyTypedStore" by auto

    have contractL:"Contract loaded = c" using loadedDef by simp
    have addressL:"Address loaded = adv" using loadedDef by simp
    have fi1:"(∃c. Type (Accounts (st⦇Gas := g'⦈) (Address loaded)) = Some (atype.Contract c) ∧ Contract loaded = c)" using contractL addressL 1 a10 a40 by simp
    have ctd:"∃dud. ep $$ Contract loaded = Some (ct, dud)" using a50 contractL 1 a10 by blast
    then have fi2:"∀id v. (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id)) = (ct $$ id = Some (Var v))"
      using loadedDef unfolding ffold_init_def using ffoldInit_var_storage_mapping_eq[of ct adv c "(Address e)" v' loaded] by auto
    then have fi3:"(∃c. Type ((acc) (Address loaded)) = Some (atype.Contract c) ∧ Contract loaded = c)"
      using fi1 contractL 1(7) transfer_type_same by simp
    have fi4:"(∀id v. ∃dud. ep $$ Contract loaded = Some (ct, dud)
                          ∧ ct $$ id = Some (Var v) = (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id)))"
      using fi1 ctd fi2
      by (metis (mono_tags, opaque_lifting))
    have fiLoaded:"fullyInitialised loaded (Accounts (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory :=
        emptyTypedStore⦈)) (Stack (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈))"
    proof -
      obtain c_fi where cfi0:"Type (acc (Address loaded)) = Some (atype.Contract c_fi) ∧ Contract loaded = c_fi"
        using fi3 by blast
      have cfi:"Type (Accounts (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈) (Address loaded))
            = Some (atype.Contract c_fi) ∧ Contract loaded = c_fi"
        using cfi0 by simp
      obtain dud_fi where epfi:"ep $$ Contract loaded = Some (ct, dud_fi)"
        using ctd by blast
      have mapfi:"∀id v. (ct $$ id = Some (Var v)) = (Denvalue loaded $$ id = Some (type.Storage v, Storeloc id))"
        using fi2 by blast
      have locfi:"∀id v loc. Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
      proof (intro allI impI)
        fix id v loc
        assume h:"Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc)"
        have hloc:"Denvalue loaded $$ id = Some (type.Storage v, Storeloc loc) ⟶ snd (type.Storage v, Storeloc loc) = Storeloc id"
          using tsLoaded' by blast
        then show "id = loc" using h by simp
      qed
      have ptrfi:"∀t l p.
          (type.Storage t, Stackloc l) |∈| fmran (Denvalue loaded) ∧
          accessStore l (Stack (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈)) = Some (KStoptr p) ⟶
          (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue loaded) ∧ CompStoType t' t l' p)"
        by (simp add: accessStore_def emptyStore_def)
      show ?thesis
        unfolding fullyInitialised_def using cfi epfi mapfi locfi ptrfi by blast
    qed
    have stmtC:"stmt f loaded emptyTypedStore (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈) = Normal ((), st'')"
      using 1(8) loadedDef unfolding ffold_init_def by simp
    have IH1:"StateInvariant loaded (st⦇Gas := g', Accounts := acc, Stack := emptyStore, Memory := emptyTypedStore⦈) st'' emptyTypedStore"
      using 8(1)[OF a1 a10 a20 a30 a40 a50 _ a60 a70 a80 _ a95 _ _ loadedDef _ _ a90 a100 tsLoaded stmtC fiLoaded ] by simp

    have acc:"(∀adv. case Type (Accounts st adv) of None ⇒ True | Some EOA ⇒ True | Some (atype.Contract c) ⇒ addressFormat adv ∧ c |∈| fmdom ep)"
      using 8(2) unfolding TypeSafe_def AddressTypes_def by blast
    obtain cOld where "Type (Accounts st (Address e)) = Some (atype.Contract cOld) ∧ Contract e = cOld"
      using 8(4) unfolding fullyInitialised_def by simp
    then obtain ctO dudO where ctoDef: "ep $$ Contract e = Some (ctO, dudO)" using acc
      using "8.prems"(3) typesafe_base.fullyInitialised_def typesafe_base_axioms by blast
    have storageFinal:"Storage st' = Storage st''" using 1 by auto

    have ct1:"∃dud. ep $$ Contract loaded = Some (ct, dud)" using a50 contractL 1 a10 by blast
    have SafeC:"safeContract (Accounts st'') (Storage st'')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast

    then have scNew:"∀e ct dud i tp. Type (Accounts st'' (Address e)) = Some (atype.Contract (environment.Contract e))
                      ∧ ep $$ Contract (e::environment) = Some (ct, dud) ∧ ct $$ i = Some (Var tp) ⟶ SCon tp i (Storage st'' (Address e))"
      using SafeC unfolding safeContract_def by simp
    have accFinal:"Accounts st' = Accounts st''" using 1 by simp

    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def using 1 by simp
      next
        show "safeContract (Accounts st') (Storage st')" using IH1 1 unfolding StateInvariant_def TypeSafe_def by auto
      next
        show "unique_locations (Denvalue e)" using "8.prems"(1) typeSafeUnique by blast
      next
        have "compPointers (Stack st'') (Denvalue loaded)"
          using IH1 unfolding StateInvariant_def TypeSafe_def by blast
        show "compPointers (Stack st') (Denvalue e)" unfolding compPointers_def
        proof intros
          fix tp1 tp2 l1 l2 l1' l2' stl1 stl2
          assume a0:"(type.Storage tp1, l1) |∈| fmran (Denvalue e) ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue e) ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2)"
          then have a5:"accessStore l1' (Stack st') = accessStore l1' (Stack st)" using 1 by simp
          have a10:"accessStore l2' (Stack st') = accessStore l2' (Stack st)" using 1 by auto
          show "if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2
                else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True"
            using 8(2) unfolding TypeSafe_def compPointers_def using a0 a5 a10 by simp
        qed
      next
        show "balanceTypes (Accounts st')" using 1 IH1 unfolding StateInvariant_def TypeSafe_def by auto
      next
        have "envAddressesWellFormed e" using "8.prems"(1) unfolding TypeSafe_def by simp
        then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
      next
        show "svalueTypes (Svalue e)" using "8.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using 1 "8.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs cd" using "8.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using 1 "8.prems"(1) unfolding TypeSafe_def by simp
      next
        show "typeCompat (Denvalue e) (Stack st') (Memory st') (Storage st' (Address e)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume tc1:"(t, l) |∈| fmran (Denvalue e)"
          show "denvalueCon (Stack st') (Memory st') (Storage st' (Address e)) cd t l"
          proof(cases l)
            case (Stackloc x1)
            then show ?thesis
            proof(cases "accessStore x1 (Stack st')")
              case None
              then show ?thesis using tc1 Stackloc using 8(2) 1 unfolding TypeSafe_def typeCompat_def by force
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 8(2) 1(9) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KCDptr x2)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 8(2) 1(9) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KMemptr x3)
                then show ?thesis apply(cases t) using tc1 Stackloc Some using 8(2) 1(9) unfolding TypeSafe_def typeCompat_def by force+
              next
                case (KStoptr x4)
                then have accO:"accessStore x1 (Stack st) = Some( KStoptr x4)"
                  using Some 1 KStoptr by auto
                then obtain struct where structDef: "t = type.Storage struct"
                  using 8(2) unfolding TypeSafe_def typeCompat_def using tc1 Some KStoptr Stackloc
                  by (cases t) fastforce+
                have tOld: "Type (Accounts st (Address e)) = Some (atype.Contract (environment.Contract e))"
                  using "8.prems"(3) unfolding fullyInitialised_def by auto
                obtain tprnt lprnt where tprntDef: "((type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e) ∧ CompStoType tprnt struct lprnt x4)"
                  using 8(4) using accO tc1 unfolding fullyInitialised_def
                  using Stackloc structDef by blast

                then have "SCon tprnt lprnt (Storage st'' (Address e))"
                  using ctoDef IH1 fi4 ct1 scNew
                    "8.prems"(2,3) tOld
                      fi_denvalue_storeloc_to_contract_var atype_same accFinal
                  by (metis (no_types, lifting))

                then have "SCon struct x4 (Storage st'' (Address e))"
                  using tprntDef SCon_imps_sublocs by blast
                then show ?thesis using Stackloc Some KStoptr structDef storageFinal by simp
              qed
            qed
          next
            case (Storeloc x2)
            then obtain struct where structDef: "t = type.Storage struct"
              using 8(2) tc1 unfolding TypeSafe_def typeCompat_def by (cases t,fastforce+)

            then have "SCon struct x2 (Storage st'' (Address e))"
              using ctoDef IH1 fi4 ct1 scNew fmlookup_ran_iff fullyInitialised_def "8.prems"(3)
            proof -
              have "Type (Accounts st (Address e)) = Some (atype.Contract (environment.Contract e))"
                using "8.prems"(3) unfolding fullyInitialised_def by auto
              then have "Type (Accounts st'' (Address e)) = Some (atype.Contract (environment.Contract e))"
                using atype_same[OF "8.prems"(2)] accFinal by simp
              moreover have "ctO $$ x2 = Some (Var struct)"
                using fi_denvalue_storeloc_to_contract_var[OF "8.prems"(3) ctoDef] tc1 Storeloc structDef by simp
              ultimately show ?thesis using scNew ctoDef by blast
            qed

            then show ?thesis using Storeloc structDef
              by (simp add: storageFinal)
          qed
        qed
      next
        show "denvalueTypeCorrectness e (Stack st') (Memory st')" using 1 8(2) unfolding TypeSafe_def by simp
      next
        show "subPrefixStructuralConsistency (Memory st')" using 1 8(2) unfolding TypeSafe_def by simp
      next
        show "SomeValSomeTyp (Memory st')" using 1 8(2) unfolding TypeSafe_def by simp
      qed
    next
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address e)) = Some (atype.Contract c) ∧
          Contract e = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p))"
        using 8(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address e)) = Some (atype.Contract c_fi) ∧
          Contract e = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address e)) = Some (atype.Contract c_fi)"
        using fiC atype_same "8.prems"(2,3) statement_with_gas.atype_same statement_with_gas_axioms by blast
      have ptrNew:"∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        using 8(4) 1 unfolding fullyInitialised_def by auto
      show "fullyInitialised e (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC fiPtr ptrNew typeNew by blast
    next
      show "⋀locs t. accessTypeStore locs (Memory st) = Some t ⟹ accessTypeStore locs (Memory st') = Some t"
        using 8(3) 1(9) by simp
    next
      show "⋀locs v.
       accessStore locs (Memory st) = Some (MPointer v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using 1(9) by simp
    next
      show "⋀locs v. accessStore locs (Memory st) = Some (MValue v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using 1(9) by simp
    next
      show "⋀i loc.
       i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) ⟹
       accessStore loc (Memory st) = None ⟹ accessStore loc (Memory st') = None"
        using 1(9) by simp
    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')" using 1(9) by simp

    qed
  next
    case (2 v t g adv g' v' acc)
    then have a1:"assert Gas (λst. costs (TRANSFER ad ex) e cd st < state.Gas st) st = Normal ((), st)" by simp
    have a10:"modify (λst. st⦇Gas := state.Gas st - costs (TRANSFER ad ex) e cd st⦈) st = Normal ((), st⦇Gas := state.Gas st - costs (TRANSFER ad ex) e cd st⦈)"
      by simp
    have a20:"toState (local.expr ad e cd) (st⦇Gas := state.Gas st - costs (TRANSFER ad ex) e cd st⦈) = Normal ((KValue adv, Value TAddr), st⦇Gas := g⦈)"
      using 2 by simp
    then have a30:"(case (KValue adv, Value TAddr) of (KValue adv, Value TAddr) ⇒ return adv | (KValue adv, Value _)
        ⇒ throw Err | (KValue adv, _) ⇒ throw Err | (_, b) ⇒ throw Err)
     (st⦇Gas := g⦈) = Normal (adv, st⦇Gas := g⦈)" using 2 by simp
    have a40:" toState (local.expr ex e cd) (st⦇Gas := g⦈) = Normal ((KValue v, Value t), st⦇Gas := g'⦈)" using 2 by auto
    have a50:"(case (KValue v, Value t) of (KValue v, Value t) ⇒ return (v, t) | (KValue v, _) ⇒ throw Err | (_, b)
        ⇒ throw Err) (st⦇Gas := g'⦈) = Normal ((v, t), (st⦇Gas := g'⦈))"
      using 2 by simp
    then have a60:"option Err (λ_. convert t (TUInt b256) v) (st⦇Gas := g'⦈) = Normal (v', st⦇Gas := g'⦈)" using 2 by simp
    have a70:"applyf Accounts (st⦇Gas := g'⦈) = Normal (Accounts (st⦇Gas := g⦈), (st⦇Gas := g'⦈))" using 2 by simp
    have a90:"option Err (λst. transfer (Address e) adv v' (Accounts st)) (st⦇Gas := g'⦈) = Normal (acc, (st⦇Gas := g'⦈))"
      using 2 by simp

    have svv':"svalueTypes v'" unfolding svalueTypes_def
      using 2(4,3) 8(2) 8(4)
      using exprTypeconInduct(3)[of ex e cd "(st⦇Gas := g⦈)" g "KValue v" "Value t" g']
        typeSafeConvert[of t v "(TUInt b256)"] convertSame by auto

    have btACC:"balanceTypes acc" unfolding balanceTypes_def
    proof -
      have old:"balanceTypes (Accounts (st⦇Gas := g'⦈))" using 8(2) unfolding TypeSafe_def by simp
      have accExp:"(case subBalance (Address e) v' (Accounts (st)) of None ⇒ None | Some x ⇒ addBalance adv v' x) = Some acc"
        using 2(6) unfolding transfer_def by blast
      have v'TC: "typeCon (TUInt b256) v'"
        using svv' unfolding svalueTypes_def by blast

      show "∀adv'. typeCon (TUInt b256) (Bal (acc adv')) "
      proof(cases "adv = (Address e)")
        case same:True
        show ?thesis using transfer_sameRead
          by (metis "2"(6) "8"(2) balanceTypes_def same typeSafeAccounts transfer_eq v'TC)
      next
        case notSame:False
        show ?thesis
        proof
          fix adv'
          show "typeCon (TUInt b256) (Bal (acc adv')) "
          proof(cases "adv' = (Address e)")
            case True
            then have tco:"typeCon (TUInt b256) (Bal ((Accounts (st⦇Gas := g'⦈)) adv'))"
              using True old accExp balanceTypes_def by blast
            have "Bal (acc (Address e)) = ShowLint (ReadLint (Bal (Accounts (st⦇Gas := g'⦈) (Address e))) - (ReadLint v'))"
              using transfer_subRead[OF 2(6) _ v'TC ] True notSame tco by auto
            then show ?thesis using True
              using "2"(6,3) Read_ShowL_id ‹typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv'))› checkUInt_def transfer_val1 transfer_val2
            proof -
              have "∀a. typeCon (TUInt b256) (Bal (Accounts st a))"
                using old unfolding balanceTypes_def by simp
              then show ?thesis using transfer_Bal_typeCon[unfolded transfer_def, OF accExp] by blast
            qed
          next
            case False
            then show ?thesis
            proof(cases "adv' = adv")
              case True
              then have "typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv))"
                using True old accExp balanceTypes_def by simp
              then have "Bal (acc adv) = (ShowLint ((ReadLint (Bal (Accounts (st⦇Gas := g'⦈) adv))) + (ReadLint v')))"
                using transfer_addRead[OF 2(6)_ v'TC ] 2(3) notSame by auto
              then show ?thesis using True
                using "2"(6,3) Read_ShowL_id ‹typeCon (TUInt b256) (Bal (Accounts (st⦇Gas := g'⦈) adv))› checkUInt_def transfer_val1 transfer_val2 notSame by auto
            next
              case f2:False
              then show ?thesis using False transfer_eq[OF 2(6)] old
                using balanceTypes_def by force
            qed
          qed
        qed
      qed
    qed

    have accfinal:"Accounts st'= acc" using 2 by simp

    have sameStores:"Storage st' = Storage st ∧ Memory st = Memory st' ∧ Stack st = Stack st'" using 2 by simp
    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts st')" unfolding AddressTypes_def
        proof intros
          fix adv
          show "case Type (Accounts st' adv) of None ⇒ True | Some EOA ⇒ True | Some (atype.Contract c) ⇒ addressFormat adv ∧ c |∈| fmdom ep"
          proof (cases "Type (Accounts st' adv)")
            case None
            then show ?thesis by simp
          next
            case (Some a)
            then show ?thesis
            proof(cases a)
              case EOA
              then show ?thesis using Some by simp
            next
              case (Contract x2)
              then show ?thesis using Some 8(2) unfolding TypeSafe_def using accfinal 2(6) transfer_type_same
                by (metis transfer_type_same accfinal Some "2"(6) AddressTypes_def)
            qed
          qed
        qed
      next
        show "safeContract (Accounts st') (Storage st')" using 8(2) 2 unfolding TypeSafe_def using sameStores
        proof -
          have "safeContract (Accounts st) (Storage st')" using 8(2) sameStores unfolding TypeSafe_def by simp
          moreover have "∀a. Type (Accounts st' a) = Type (Accounts st a)"
            using transfer_type_same[OF "2"(6)] accfinal by simp
          ultimately show ?thesis by (rule safeContract_Type_cong)
        qed
      next
        show "unique_locations (Denvalue e)" using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "compPointers (Stack st') (Denvalue e)" using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "balanceTypes (Accounts st')" using btACC accfinal by simp
      next
        have "envAddressesWellFormed e" using "8.prems"(1) unfolding TypeSafe_def by simp
        then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
      next
        show "svalueTypes (Svalue e)" using "8.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "lessThanTopLocs cd" using "8.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "typeCompat (Denvalue e) (Stack st') (Memory st') (Storage st' (Address e)) cd"
          using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "denvalueTypeCorrectness e (Stack st') (Memory st')" using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "subPrefixStructuralConsistency (Memory st')" using 8(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "SomeValSomeTyp (Memory st')" using 8(2) unfolding TypeSafe_def using sameStores by simp
      qed
    next
      have fiOld:"
              ∃c ct dud.
                Type (Accounts st (Address e)) = Some (atype.Contract c) ∧
                Contract e = c ∧
                ep $$ c = Some (ct, dud) ∧
                (∀id v. (ct $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
                (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
                (∀t l p.
                    (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
                    (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p))"
        using 8(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address e)) = Some (atype.Contract c_fi) ∧
          Contract e = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address e)) = Some (atype.Contract c_fi)"
        using fiC atype_same "8.prems"(2,3) statement_with_gas.atype_same statement_with_gas_axioms by blast
      have ptrNew:"∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        using 8(4) 2 unfolding fullyInitialised_def by auto
      show "fullyInitialised e (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC fiPtr ptrNew typeNew by blast
    next
      show "⋀locs t. accessTypeStore locs (Memory st) = Some t ⟹ accessTypeStore locs (Memory st') = Some t"
        using 8(3) 2(7) by simp
    next
      show "⋀locs v.
       accessStore locs (Memory st) = Some (MPointer v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using 2(7) by simp
    next
      show "⋀locs v. accessStore locs (Memory st) = Some (MValue v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using 2(7) by simp
    next
      show "⋀i loc.
       i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) ⟹
       accessStore loc (Memory st) = None ⟹ accessStore loc (Memory st') = None"
        using 2(7) by simp
    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')" using 2(7) by simp
    qed
  qed
next
  case (9 id0 tp s ev cd st)
  show ?case
  proof(cases rule:blockNone[OF 9(3)])
    case (1 cd' mem' sck' e')
    have a10:"assert Gas (λst. costs (BLOCK (id0, tp, None) s) ev cd st < state.Gas st) st = Normal ((), st)" using 1 by simp
    obtain sgas where sgasDef:"sgas = state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st" by simp
    then have a20:"modify (λst. st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈) st = Normal ((), (st⦇Gas := sgas⦈ ))" by simp
    have a30:"option Err (λst. decl id0 tp None False cd (Memory st) (Storage st (Address ev)) (cd, Memory st, Stack st, ev)) (st⦇Gas := sgas⦈ )
            = Normal ((cd', mem', sck', e'), (st⦇Gas := sgas⦈ )) " using 1 by simp
    have a40:"modify (λst. st⦇Stack := sck', Memory := mem'⦈) (st⦇Gas := sgas⦈) = Normal ((), st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)"
      by simp

    have tsDecl:"TypeSafe e' (Accounts (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) sck' mem'
     (Storage (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) cd' ∧
    (∀x l. (∄y y'. x = type.Memory y ∨ x = Value y') ∧ (x, l) |∈| fmran (Denvalue e') ⟶
           (x, l) |∈| fmran (Denvalue ev)) ∧
    (∀sckl ptr.
        accessStore sckl sck' = Some ptr ∧ (∄y y'. ptr = KMemptr y ∨ ptr = KValue y') ⟶
        accessStore sckl (Stack (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) =
        Some ptr) ∧
    cd = cd'∧
    Toploc (Memory (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) ≤ Toploc mem' ∧
    (∀locs v.
        accessStore locs (Memory (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) = Some v ⟶
        accessStore locs mem' = Some v) ∧
    (∀locs t.
        accessTypeStore locs (Memory (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) = Some t ⟶
        accessTypeStore locs mem' = Some t) ∧
    (∀locs.
        (∃tloc<Toploc (Memory (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)).
            LSubPrefL2 locs (ShowLnat tloc) ∧
            accessStore locs (Memory (st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, None) s) ev cd st⦈)) = None) ⟶
        accessStore locs mem' = None)"
      using typeSafeDeclNone[OF _ 1(2)] 9(2) by simp

    then have a50:"TypeSafe e' (Accounts (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)) (Stack (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈))
   (Memory (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)) (Storage (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)) cd'" by simp
    have denLink:"∀ii x. Denvalue ev $$ ii = Some x ⟶ Denvalue e' $$ ii = Some x" using decl_env_monotonic[OF 1(2)] by auto

    have fiDec:" fullyInitialised e' (Accounts (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)) (Stack (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈))"
    proof -
      have sameStuff:"Address e' = Address ev ∧ Contract e' = Contract ev"
        using 1(2) decl_env by simp
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address ev)) = Some (atype.Contract c) ∧
          Contract ev = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p))"
        using 9(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address ev)) = Some (atype.Contract c_fi) ∧
          Contract ev = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p)"
        by blast

      have cfi:"Type (Accounts (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈) (Address e')) = Some (atype.Contract c_fi) ∧
          Contract e' = c_fi ∧ ep $$ c_fi = Some (ct_fi, dud_fi)"
        using fiC sameStuff by simp

      have mapfi:"∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e' $$ id = Some (type.Storage v, Storeloc id))"
      proof (intro allI)
        fix id v
        show "(ct_fi $$ id = Some (Var v)) = (Denvalue e' $$ id = Some (type.Storage v, Storeloc id))"
        proof
          assume hct:"ct_fi $$ id = Some (Var v)"
          then have "Denvalue ev $$ id = Some (type.Storage v, Storeloc id)"
            using fiC by blast
          then show "Denvalue e' $$ id = Some (type.Storage v, Storeloc id)"
            using decl_env_monotonic[OF 1(2)] by blast
        next
          assume hnew:"Denvalue e' $$ id = Some (type.Storage v, Storeloc id)"
          show "ct_fi $$ id = Some (Var v)"
          proof (cases "id = id0")
            case True
            have "∀t l. Denvalue e' $$ id0 = Some (t, Storeloc l) ⟶ Denvalue ev $$ id0 = Some (t, Storeloc l)"
              using decl_env_storlocs_unchanged[OF 1(2)] by simp
            then have "Denvalue ev $$ id0 = Some (type.Storage v, Storeloc id0)"
              using hnew True by blast
            then show ?thesis using fiC True by blast
          next
            case False
            then have "Denvalue ev $$ id = Some (type.Storage v, Storeloc id)"
              using decl_env_not_i[OF 1(2)] hnew by blast
            then show ?thesis using fiC by blast
          qed
        qed
      qed

      have locfi:"∀id v loc. Denvalue e' $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
      proof (intro allI impI)
        fix id v loc
        assume hnew:"Denvalue e' $$ id = Some (type.Storage v, Storeloc loc)"
        show "id = loc"
        proof (cases "id = id0")
          case True
          have "∀t l. Denvalue e' $$ id0 = Some (t, Storeloc l) ⟶ Denvalue ev $$ id0 = Some (t, Storeloc l)"
            using decl_env_storlocs_unchanged[OF 1(2)] by simp
          then have "Denvalue ev $$ id0 = Some (type.Storage v, Storeloc loc)"
            using hnew True by blast
          then have "id0 = loc" using fiC by blast
          then show ?thesis using True by simp
        next
          case False
          then have "Denvalue ev $$ id = Some (type.Storage v, Storeloc loc)"
            using decl_env_not_i[OF 1(2)] hnew by blast
          then show ?thesis using fiC by blast
        qed
      qed

      have ptrfi:"∀t l p.
          (type.Storage t, Stackloc l) |∈| fmran (Denvalue e') ∧
          accessStore l (Stack (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)) = Some (KStoptr p) ⟶
          (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e') ∧ CompStoType t' t l' p)"
      proof (intro allI impI)
        fix t l p
        assume in1:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue e') ∧
            accessStore l (Stack (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈)) = Some (KStoptr p)"
        have inOld:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue ev)" using tsDecl in1 by simp
        have accOld:"accessStore l (Stack st) = Some (KStoptr p)" using tsDecl in1 by simp
        from fiPtr inOld accOld obtain t' l' where oldRan:
            "(type.Storage t', Storeloc l') |∈| fmran (Denvalue ev)"
            and comp:"CompStoType t' t l' p"
          by blast
        from oldRan obtain idp where idpDef:"Denvalue ev $$ idp = Some (type.Storage t', Storeloc l')"
          using fmlookup_ran_iff by fast
        have "Denvalue e' $$ idp = Some (type.Storage t', Storeloc l')"
          using decl_env_monotonic[OF 1(2)] idpDef by blast
        then have newRan:"(type.Storage t', Storeloc l') |∈| fmran (Denvalue e')"
          by (meson fmranI)
        show "∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e') ∧ CompStoType t' t l' p"
          using newRan comp by blast
      qed

      show ?thesis
        unfolding fullyInitialised_def using cfi mapfi locfi ptrfi by blast
    qed

    have IH1:"StateInvariant e' (st⦇Gas := sgas, Stack := sck', Memory := mem'⦈) st' cd'"
      using 9(1)[OF a10 a20 a30, of cd' "(mem', sck', e')" mem' "(sck', e')" sck' e', OF _ _ _ a40 a50 _ fiDec, of st'] 1(3) sgasDef by simp
    then have fiFinal:" fullyInitialised e' (Accounts st') (Stack st')"
      unfolding StateInvariant_def by blast
    have sameAdd:"Address e' = Address ev" using 1(2) decl_env by simp
    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe ev (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "safeContract (Accounts st') (Storage st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "unique_locations (Denvalue ev)" using 9(2) unfolding TypeSafe_def by simp
      next
        have cpNew:"(∀tp1 tp2 l1 l2 l1' l2' stl1 stl2.
       (type.Storage tp1, l1) |∈| fmran (Denvalue e') ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue e') ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2) ⟶
       (if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2 else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True))"
          using IH1 unfolding StateInvariant_def TypeSafe_def compPointers_def by auto
        show "compPointers (Stack st') (Denvalue ev)" unfolding compPointers_def
        proof intros
          fix tp1 tp2 l1 l2 l1' l2' stl1 stl2
          assume a0:"(type.Storage tp1, l1) |∈| fmran (Denvalue ev) ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue ev) ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2)"
          then obtain i1 i2 where iDefs:" (Denvalue ev) $$ i1 = Some (type.Storage tp1, l1)
                                  ∧ (Denvalue ev) $$ i2 = Some (type.Storage tp2, l2)" by blast
          then have "(type.Storage tp1, l1) |∈| fmran (Denvalue e') ∧ (type.Storage tp2, l2) |∈| fmran (Denvalue e')" using a0
            by (metis denLink fmlookup_ran_iff)
          then show "if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2
                else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True"
            using cpNew using a0 by blast
        qed
      next
        show "balanceTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        have "envAddressesWellFormed ev" using 9(2) unfolding TypeSafe_def by simp
        then show "addressFormat (Address ev)" and "addressFormat (Sender ev)" by simp+
      next
        show "svalueTypes (Svalue ev)" using 9(2) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "lessThanTopLocs cd" using 9(2) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        have oldTC:"typeCompat (Denvalue e') (Stack st') (Memory st') (Storage st' (Address e')) cd'"
          using IH1 unfolding StateInvariant_def TypeSafe_def by blast
        show "typeCompat (Denvalue ev) (Stack st') (Memory st') (Storage st' (Address ev)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume inDen:"(t, l) |∈| fmran (Denvalue ev)"
          then have inDenN:"(t, l) |∈| fmran (Denvalue e')"
            using denLink by (meson fmlookup_ran_iff)

          show "denvalueCon (Stack st') (Memory st') (Storage st' (Address ev)) cd t l"
          proof(cases l)
            case (Stackloc x1)
            then have oldTC':"(
             case accessStore x1 (Stack st') of None ⇒ False
              | Some (KValue val) ⇒ (case t of Value typ ⇒ typeCon typ val | _ ⇒ False)
              | Some (KCDptr stloc) ⇒ (case t of Calldata struct ⇒ MCon struct cd' stloc | _ ⇒ False)
              | Some (KMemptr stloc) ⇒ (case t of type.Memory struct ⇒ MCon struct (Memory st') stloc | _ ⇒ False)
              | Some (KStoptr stloc) ⇒ (case t of type.Storage struct ⇒ SCon struct stloc (Storage st' (Address e')) | _ ⇒ False))"
              using oldTC unfolding typeCompat_def using inDenN by fastforce
            then show ?thesis
            proof(cases "accessStore x1 (Stack st')")
              case None
              then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd Stackloc by fastforce
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1')
                then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd Some Stackloc by force
              next
                case (KCDptr x2)
                then obtain struct where structDef:"t = Calldata struct ∧ MCon struct cd' x2"
                  using oldTC' unfolding typeCompat_def using inDenN sameAdd Some Stackloc by (cases t; auto)
                then show ?thesis using tsDecl KCDptr Some Stackloc by simp
              next
                case (KMemptr x3)
                then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd Some Stackloc by fastforce
              next
                case (KStoptr x4)
                then have "case t of type.Storage struct ⇒ SCon struct x4 (Storage st' (Address e')) | _ ⇒ False"
                  using oldTC' using inDenN sameAdd Some Stackloc by (auto split:option.splits type.splits stackvalue.splits denvalue.splits)
                then obtain struct where "t = type.Storage struct ∧ SCon struct x4 (Storage st' (Address e'))" by (cases t; auto)
                then show ?thesis using Some Stackloc KStoptr
                  by (simp add: sameAdd)
              qed
            qed
          next
            case (Storeloc x2)
            then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd
              by (metis (no_types, lifting) denvalue.simps(6) type.exhaust type.simps(17,18,19,20))
          qed
        qed
      next
        show "denvalueTypeCorrectness ev (Stack st') (Memory st')"
          unfolding denvalueTypeCorrectness_def
        proof intros
          fix t l ptr_loc sub_loc
          assume in1:"(type.Memory t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st') = Some (KMemptr ptr_loc)"
          then have in2:"(type.Memory t, Stackloc l) |∈| fmran (Denvalue e')" using denLink
            by (meson fmlookup_ran_iff)
          then show " case t of MTArray len arr ⇒ ∀i<len. accessTypeStore (hash ptr_loc (ShowLnat i)) (Memory st') = Some arr
       | MTValue val ⇒ accessTypeStore ptr_loc (Memory st') = Some (MTValue val)"
            using IH1 unfolding StateInvariant_def TypeSafe_def denvalueTypeCorrectness_def
            using in1 in2 by blast
        qed
      next
        show "subPrefixStructuralConsistency (Memory st')" using IH1 unfolding StateInvariant_def TypeSafe_def by simp
      next
        show "SomeValSomeTyp (Memory st')" using IH1 unfolding StateInvariant_def TypeSafe_def by simp
      qed
    next
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address ev)) = Some (atype.Contract c) ∧
          Contract ev = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p))"
        using 9(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address ev)) = Some (atype.Contract c_fi) ∧
          Contract ev = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address ev)) = Some (atype.Contract c_fi)"
        using fiC atype_same "9.prems"(2,3) statement_with_gas.atype_same statement_with_gas_axioms by blast
      have ptrNew:"∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p)"
      proof (intro allI impI)
        fix t l p
        assume in1:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st') = Some (KStoptr p)"
        have inNew:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue e')"
          using denLink in1 by (meson fmlookup_ran_iff)
        from fiFinal inNew in1 obtain t' l' where newPtr:
            "(type.Storage t', Storeloc l') |∈| fmran (Denvalue e')"
            and comp:"CompStoType t' t l' p"
          unfolding fullyInitialised_def by blast
        from newPtr obtain iid where iidDef:"Denvalue e' $$ iid = Some (type.Storage t', Storeloc l')"
          using fmlookup_ran_iff by fast
        have oldLookup:"Denvalue ev $$ iid = Some (type.Storage t', Storeloc l')"
        proof (cases "iid = id0")
          case True
          have "∀t l. Denvalue e' $$ id0 = Some (t, Storeloc l) ⟶ Denvalue ev $$ id0 = Some (t, Storeloc l)"
            using decl_env_storlocs_unchanged[OF 1(2)] by simp
          then show ?thesis using iidDef True by blast
        next
          case False
          then show ?thesis using decl_env_not_i[OF 1(2) iidDef] by blast
        qed
        have oldRan:"(type.Storage t', Storeloc l') |∈| fmran (Denvalue ev)"
          using oldLookup by (meson fmranI)
        show "∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p"
          using oldRan comp by blast
      qed
      show "fullyInitialised ev (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC fiPtr ptrNew typeNew by blast
    next
      fix locs t
      assume in1:"accessTypeStore locs (Memory st) = Some t"
      then have same:"accessTypeStore locs (Memory st) = accessTypeStore locs mem'"
        using tsDecl in1 by simp
      then have "accessTypeStore locs (mem') = Some t"
        using in1 by simp
      then show "accessTypeStore locs (Memory st') = Some t"
        using in1 IH1 unfolding StateInvariant_def by simp
    next
      fix locs v
      assume in1:"accessStore locs (Memory st) = Some (MPointer v)"
      then have same:"accessStore locs (Memory st) = accessStore locs mem'"
        using tsDecl by simp
      then have "accessStore locs (mem') = Some (MPointer v)"
        using in1 by simp
      then show "∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using in1 IH1 unfolding StateInvariant_def by simp
    next
      fix locs v
      assume in1:"accessStore locs (Memory st) = Some (MValue v)"
      then have same:"accessStore locs (Memory st) = accessStore locs mem'"
        using tsDecl unfolding ncpOMemInDMem_def by simp
      then have "accessStore locs (mem') = Some (MValue v)"
        using in1 by simp
      then show "∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using in1 IH1 unfolding StateInvariant_def by simp
    next
      fix i loc
      assume in1:"i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) "
        and in2:" accessStore loc (Memory st) = None"
      moreover have "Toploc (Memory st) ≤ Toploc mem'" using tsDecl unfolding ncpOMemInDMem_def by simp
      then have same:"accessStore loc (Memory st) = accessStore loc mem'"
        using tsDecl in1 by fastforce
      moreover have "i < Toploc (mem') ∧ LSubPrefL2 loc (ShowLnat i)"
        using calculation in1
        using ‹Toploc (Memory st) ≤ Toploc mem'› order.strict_trans2 by blast
      ultimately have "accessStore loc mem' = None ⟶
        accessStore loc (Memory st') = None" using IH1 unfolding StateInvariant_def by simp
      then show "accessStore loc (Memory st') = None" using in2 same by simp

    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')"
        using 1 IH1 unfolding StateInvariant_def using tsDecl by simp
    qed
  qed
next
  case (10 id0 tp ex' s ev cd st)
  show ?case
  proof(cases rule:blockSome[OF 10(3)])
    case (1 v t g cd' mem' sck' e')
    have a10:"assert Gas (λst. costs (BLOCK (id0, tp, Some ex') s) ev cd st < state.Gas st) st = Normal ((), st)" using 1 by simp
    obtain sgas where sgasDef:"sgas = state.Gas st - costs (BLOCK (id0, tp, Some ex') s) ev cd st" by simp
    then have a20:"modify (λst. st⦇Gas := state.Gas st - costs (BLOCK (id0, tp, Some ex') s) ev cd st⦈) st = Normal ((), (st⦇Gas := sgas⦈ ))" by simp
    have a25:"toState (local.expr ex' ev cd) (st⦇Gas := sgas⦈ ) = Normal ((v, t), (st⦇Gas := g⦈))" using 1 sgasDef by auto
    have a30:"option Err (λst. decl id0 tp (Some (v, t)) False cd (Memory st) (Storage st (Address ev)) (cd, Memory st, Stack st, ev)) (st⦇Gas := g⦈ )
            = Normal ((cd', mem', sck', e'), (st⦇Gas := g⦈ )) " using 1 by simp
    have a40:"modify (λst. st⦇Stack := sck', Memory := mem'⦈) (st⦇Gas := g⦈) = Normal ((), st⦇Gas := g, Stack := sck', Memory := mem'⦈)"
      by simp

    have "∃c ct dud.
       Type (Accounts st (Address ev)) = Some (atype.Contract c) ∧
       environment.Contract ev = c ∧
       ep $$ c = Some (ct, dud) ∧
       (∀id v. (ct $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
       (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
       (∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p))"
      using sgasDef 10(4) unfolding fullyInitialised_def by simp
    moreover have "fullyInitialised ev (Accounts (st⦇Gas := sgas⦈)) (Stack (st⦇Gas := sgas⦈))"
      by (simp add: "10.prems"(3))
    moreover have tsSgas:"TypeSafe ev (Accounts (st⦇Gas := sgas⦈)) (Stack (st⦇Gas := sgas⦈)) (Memory
        (st⦇Gas := sgas⦈)) (Storage (st⦇Gas := sgas⦈)) cd" using 10(2) by simp
    ultimately have tcTV:"exprResultCon ev cd (st⦇Gas := sgas⦈) v t"
      using 1(2) exprTypeconInduct(3)[of ex' "ev" cd "st⦇Gas := sgas⦈" sgas v t g] sgasDef by blast
    then have tcTV':"valueCon ev cd (st⦇Gas := g⦈) v t"
      by (cases t, simp+)

    have tsDecl:"TypeSafe e' (Accounts (st⦇Gas := g⦈)) sck' mem' (Storage (st⦇Gas := g⦈)) cd' ∧
    (¬ False ⟶
     (∀locs tp. MCon tp (Memory (st⦇Gas := g⦈)) locs ⟶ MCon tp mem' locs) ∧
     ncpDenvalueLimit e' ev sck' (Stack (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) ∧
     ncpOMemInDMem (Memory (st⦇Gas := g⦈)) mem' ∧ ncpElementsNoSubPref (Memory (st⦇Gas := g⦈)) mem'
∧ ncpNewSelfPoint (Memory (st⦇Gas := g⦈)) mem') ∧ Toploc (Memory (st⦇Gas := g⦈)) ≤ Toploc mem'"
    proof -
      have tsgas:"TypeSafe ev (Accounts (st⦇Gas := g⦈)) (Stack (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) (Storage (st⦇Gas := g⦈)) cd"
        using 10(2) by simp
      have "ncpDenvalueLimit ev ev (Stack (st⦇Gas := g⦈)) (Stack (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈))"
        unfolding ncpDenvalueLimit_def by auto
      moreover have "ncpOMemInDMem (Memory (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈))" unfolding ncpOMemInDMem_def by auto
      moreover have "ncpElementsNoSubPref (Memory (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈))"
        using ncpElementsNoSubPref_def ncpElementsNoSubPref_sameMem tsgas by blast
      moreover have "ncpNewSelfPoint (Memory (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈))"
        unfolding ncpNewSelfPoint_def by auto
      ultimately have acc0:"¬ False ⟶
    (∀locs tp. MCon tp (Memory (st⦇Gas := g⦈)) locs ⟶ MCon tp (Memory (st⦇Gas := g⦈)) locs) ∧
    Toploc (Memory (st⦇Gas := g⦈)) ≤ Toploc (Memory (st⦇Gas := g⦈)) ∧
    ncpDenvalueLimit ev ev (Stack (st⦇Gas := g⦈)) (Stack (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) ∧
    ncpOMemInDMem (Memory (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) ∧
    ncpElementsNoSubPref (Memory (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) ∧ ncpNewSelfPoint (Memory
        (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈))" by blast
      have acc1:"∀struct.
     t = type.Memory struct ⟶
     (∃stloc tp'' p.
         (type.Memory tp'', Stackloc stloc) |∈| fmran (Denvalue ev) ∧
         accessStore stloc (Stack (st⦇Gas := g⦈)) = Some (KMemptr p) ∧
         (tp'' = struct ∧ v = KMemptr p ∨
          (∃len arr. p ≠ extractValueType v ∧ tp'' = MTArray len arr ∧ CompMemType (Memory (st⦇Gas := g⦈)) len arr struct p (extractValueType v))))"
        using tcTV by (cases t, blast+; fastforce)
      have acc2:"∀struct.
     t = Calldata struct ⟶
     (∃stloc tp'' p.
         (Calldata tp'', Stackloc stloc) |∈| fmran (Denvalue ev) ∧
         accessStore stloc (Stack (st⦇Gas := g⦈)) = Some (KCDptr p) ∧
         (tp'' = struct ∧ v = KCDptr p ∨ (∃len arr. p ≠ extractValueType v ∧ tp'' = MTArray len arr ∧ CompMemType cd len arr struct p (extractValueType v))))"
        using tcTV by (cases t, blast+; fastforce)
      have acc3:"∀struct.
       tp = type.Storage struct ⟶
       (∀locs tp. SCon tp locs (Storage (st⦇Gas := g⦈) (Address ev)) ⟶ SCon tp locs (Storage (st⦇Gas := g⦈) (Address ev))) ∧
       (∃stloc tp''.
           (type.Storage tp'', Storeloc stloc) |∈| fmran (Denvalue ev) ∧
           (tp'' = struct ∧ v = KStoptr stloc ∨ extractValueType v ≠ stloc ∧ CompStoType tp'' struct stloc (extractValueType v)))"
        using tcTV
      proof(cases t)
        case (Value x1)
        then have "∀struct. tp ≠ type.Storage struct " using 1(3) decl_KValue_tp_match
          by blast
        then show ?thesis by simp
      next
        case (Calldata x2)
        then have "∀struct. tp ≠ type.Storage struct " using 1(3) decl_Calldata_tp_match
          by blast
        then show ?thesis by simp
      next
        case (Memory x3)
        then have "∀struct. tp ≠ type.Storage struct " using 1(3) decl_Memory_tp_match
          by blast
        then show ?thesis by simp
      next
        case (Storage x4)
        then have sc:"SCon x4 (extractValueType v) (Storage (st⦇Gas := sgas⦈) (Address ev)) ∧
              (∃xx. v = KStoptr xx) ∧
              (∃stloc tp''.
                  (type.Storage tp'', Storeloc stloc) |∈| fmran (Denvalue ev) ∧
                  tp'' = x4 ∧ v = KStoptr stloc ∨ extractValueType v ≠ stloc ∧ CompStoType tp'' x4 stloc (extractValueType v))"
          using tcTV by auto
        have tt:"(∃x t' t''. x4 = STArray x t' ∧ cps2mTypeCompatible (STArray x t') (MTArray x t'')∧ tp = type.Memory (MTArray x t'')) ∨ tp = type.Storage x4"
          using decl_Memory_tp_options[] 1(3) Storage by simp
        then show ?thesis
        proof(cases "tp = type.Storage x4")
          case True
          moreover have "(∀locs tp. SCon tp locs (Storage (st⦇Gas := g⦈) (Address ev)) ⟶ SCon
              tp locs (Storage (st⦇Gas := g⦈) (Address ev)))" by auto
          moreover have "(Stack (st⦇Gas := sgas⦈)) = (Stack (st⦇Gas := g⦈))" by auto
          then have "(∃stloc tp''.
           (type.Storage tp'', Storeloc stloc) |∈| fmran (Denvalue ev) ∧
           tp'' = x4 ∧ v = KStoptr stloc ∨ extractValueType v ≠ stloc ∧ CompStoType tp'' x4 stloc (extractValueType v))"
            using sc True Storage by presburger
          ultimately show ?thesis
            using Storage tcTV by auto
        next
          case False
          then have "(∃x t' t''. x4 = STArray x t' ∧ cps2mTypeCompatible (STArray x t') (MTArray x t'')∧ tp = type.Memory (MTArray x t''))"
            using tt by simp
          then show ?thesis by auto
        qed
      qed
      have " TypeSafe e' (Accounts (st⦇Gas := g⦈)) sck' mem' (Storage (st⦇Gas := g⦈)) cd' ∧
    (¬ False ⟶
     (∀locs tp. MCon tp (Memory (st⦇Gas := g⦈)) locs ⟶ MCon tp mem' locs) ∧
     Toploc (Memory (st⦇Gas := g⦈)) ≤ Toploc mem' ∧
     ncpDenvalueLimit e' ev sck' (Stack (st⦇Gas := g⦈)) (Memory (st⦇Gas := g⦈)) ∧
     ncpOMemInDMem (Memory (st⦇Gas := g⦈)) mem' ∧
     ncpElementsNoSubPref (Memory (st⦇Gas := g⦈)) mem' ∧
     ncpNewSelfPoint (Memory (st⦇Gas := g⦈)) mem')
    "
        using typeSafeDecl[OF tsgas tcTV' tsgas 1(3) acc0 acc1 acc2 ] acc3 by fastforce
      then show ?thesis by simp
    qed


    then have a50:"TypeSafe e' (Accounts (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) (Stack (st⦇Gas := g, Stack := sck', Memory := mem'⦈))
   (Memory (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) (Storage (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) cd'"
      using tsDecl by simp

    have fiDec:" fullyInitialised e' (Accounts (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) (Stack (st⦇Gas := g, Stack := sck', Memory := mem'⦈))"
    proof -
      have sameStuff:"Address e' = Address ev ∧ Contract e' = Contract ev" using 1(3) decl_env by simp
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address ev)) = Some (atype.Contract c) ∧
          Contract ev = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p))"
        using 10(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address ev)) = Some (atype.Contract c_fi) ∧
          Contract ev = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p)"
        by blast

      have cfi:"Type (Accounts (st⦇Gas := g, Stack := sck', Memory := mem'⦈) (Address e')) = Some (atype.Contract c_fi) ∧
          Contract e' = c_fi ∧ ep $$ c_fi = Some (ct_fi, dud_fi)"
        using fiC sameStuff by simp

      have mapfi:"∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e' $$ id = Some (type.Storage v, Storeloc id))"
      proof (intro allI)
        fix id v
        show "(ct_fi $$ id = Some (Var v)) = (Denvalue e' $$ id = Some (type.Storage v, Storeloc id))"
        proof
          assume hct:"ct_fi $$ id = Some (Var v)"
          then have "Denvalue ev $$ id = Some (type.Storage v, Storeloc id)"
            using fiC by blast
          then show "Denvalue e' $$ id = Some (type.Storage v, Storeloc id)"
            using decl_env_monotonic[OF 1(3)] by blast
        next
          assume hnew:"Denvalue e' $$ id = Some (type.Storage v, Storeloc id)"
          show "ct_fi $$ id = Some (Var v)"
          proof (cases "id = id0")
            case True
            have "∀t l. Denvalue e' $$ id0 = Some (t, Storeloc l) ⟶ Denvalue ev $$ id0 = Some (t, Storeloc l)"
              using decl_env_storlocs_unchanged[OF 1(3)] by simp
            then have "Denvalue ev $$ id0 = Some (type.Storage v, Storeloc id0)"
              using hnew True by blast
            then show ?thesis using fiC True by blast
          next
            case False
            then have "Denvalue ev $$ id = Some (type.Storage v, Storeloc id)"
              using decl_env_not_i[OF 1(3)] hnew by blast
            then show ?thesis using fiC by blast
          qed
        qed
      qed

      have locfi:"∀id v loc. Denvalue e' $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
      proof (intro allI impI)
        fix id v loc
        assume hnew:"Denvalue e' $$ id = Some (type.Storage v, Storeloc loc)"
        show "id = loc"
        proof (cases "id = id0")
          case True
          have "∀t l. Denvalue e' $$ id0 = Some (t, Storeloc l) ⟶ Denvalue ev $$ id0 = Some (t, Storeloc l)"
            using decl_env_storlocs_unchanged[OF 1(3)] by simp
          then have "Denvalue ev $$ id0 = Some (type.Storage v, Storeloc loc)"
            using hnew True by blast
          then have "id0 = loc" using fiC by blast
          then show ?thesis using True by simp
        next
          case False
          then have "Denvalue ev $$ id = Some (type.Storage v, Storeloc loc)"
            using decl_env_not_i[OF 1(3)] hnew by blast
          then show ?thesis using fiC by blast
        qed
      qed

      have ptrfi:"∀tt l p.
          (type.Storage tt, Stackloc l) |∈| fmran (Denvalue e') ∧
          accessStore l (Stack (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = Some (KStoptr p) ⟶
          (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e') ∧ CompStoType t' tt l' p)"
      proof (intro allI impI)
        fix tt l p
        assume in1:"(type.Storage tt, Stackloc l) |∈| fmran (Denvalue e')∧accessStore l (Stack (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = Some (KStoptr p)"
        then obtain idd where iddDef: "Denvalue e' $$ idd = Some (type.Storage tt, Stackloc l)" by auto
        show " ∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e') ∧ CompStoType t' tt l' p"
        proof(cases "idd = id0")
          case True
          then have ddd:"Denvalue e' $$ id0 = Some (type.Storage tt, Stackloc l)" using iddDef by simp
          then show ?thesis
          proof(cases "Denvalue ev $$ id0")
            case None
            have ldef:"l = (ShowLnat (Toploc (Stack (st⦇Gas := g⦈))))" using decl_stack_top[OF 1(3) None] iddDef True by simp
            then obtain p2 where p2Def:"Some (v, t) = Some (KStoptr p2, type.Storage tt)"
              using ddd True decl_storage_tp[OF 1(3) None, of tt ] by blast
            then have dcc:"decl id0 tp (Some (KStoptr p2, type.Storage tt)) False cd (Memory (st⦇Gas := g⦈)) (Storage (st⦇Gas := g⦈) (Address ev))
       (cd, Memory (st⦇Gas := g⦈), Stack (st⦇Gas := g⦈), ev) =
      Some (cd', mem', sck', e')" using 1(3) True by simp
            then have "v = KStoptr p" using decl_stack_topLoc[OF dcc None] using in1 ldef p2Def by auto
            then have "(∃stloc tp''.
            (type.Storage tp'', Storeloc stloc) |∈| fmran (Denvalue ev) ∧
            (tp'' = tt ∧ v = KStoptr stloc ∨ p ≠ stloc ∧ CompStoType tp'' tt stloc p))"
              using tcTV 1(3) p2Def by auto
            then show ?thesis using decl_env_monotonic[OF 1(3)]
              by (metis CompStoType_sameLocNdTyp stackvalue.inject(4) ‹v = KStoptr p› fmlookup_ran_iff)
          next
            case (Some a)
            then have "ev = e' ∧ sck' = Stack st" using decl_some_same[OF 1(3)] by auto
            then have oldIn:"(type.Storage tt, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p)"
              using in1 by simp
            then show ?thesis using fiPtr
              using ‹ev = e' ∧ sck' = Stack st› by blast
          qed
        next
          case False
          then have oldD:"Denvalue ev $$ idd = Some (type.Storage tt, Stackloc l)"
            using iddDef in1 1 decl_env_not_i[OF 1(3)] by blast
          then show ?thesis
          proof(cases "l = ShowLnat (Toploc (Stack st))")
            case True
            then show ?thesis
              by (metis "10.prems"(1) True oldD snd_conv stackLocs_imp_NotDen TypeSafe_def)
          next
            case False
            then have "accessStore l (Stack (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = accessStore l (Stack (st))"
              using decl_stack_change[OF 1(3)] by simp
            then have oldAcc:"accessStore l (Stack st) = Some (KStoptr p)" using in1 by simp
            from fiPtr oldD oldAcc obtain t' l' where t'Def: "(type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' tt l' p"
              by (meson fmranI)
            then have "(type.Storage t', Storeloc l') |∈| fmran (Denvalue e')"
              using decl_env_monotonic[OF 1(3)] by (meson fmlookup_ran_iff)
            then show ?thesis using t'Def by auto
          qed
        qed
      qed
      show ?thesis
        unfolding fullyInitialised_def using cfi mapfi locfi ptrfi by blast
    qed
    have sameCD:"cd = cd'" using decl_env_false_same_cd[OF 1(3)] by simp
    have IH1:" StateInvariant e' (st⦇Gas := g, Stack := sck', Memory := mem'⦈) st' cd'"
      using 10(1)[OF a10 a20 a25 _ a30, of cd' "(mem', sck', e')" mem' "(sck', e')" sck' e', OF _ _ _ _ a40 a50 _ fiDec, of st'] 1(4) sgasDef by simp

    have denLink:"∀ii x. Denvalue ev $$ ii = Some x ⟶ Denvalue e' $$ ii = Some x" using decl_env_monotonic[OF 1(3)] by auto
    have sameAdd:"Address e' = Address ev"using 1(3) decl_env by simp
    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe ev (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "safeContract (Accounts st') (Storage st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "unique_locations (Denvalue ev)" using 10(2) unfolding TypeSafe_def by simp
      next
        have cpNew:"(∀tp1 tp2 l1 l2 l1' l2' stl1 stl2.
       (type.Storage tp1, l1) |∈| fmran (Denvalue e') ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue e') ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2) ⟶
       (if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2 else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True))"
          using IH1 unfolding StateInvariant_def TypeSafe_def compPointers_def by auto
        show "compPointers (Stack st') (Denvalue ev)" unfolding compPointers_def
        proof intros
          fix tp1 tp2 l1 l2 l1' l2' stl1 stl2
          assume a0:"(type.Storage tp1, l1) |∈| fmran (Denvalue ev) ∧
       (type.Storage tp2, l2) |∈| fmran (Denvalue ev) ∧
       (l1 = Stackloc l1' ∧ accessStore l1' (Stack st') = Some (KStoptr stl1) ∨ l1 = Storeloc stl1) ∧
       (l2 = Stackloc l2' ∧ accessStore l2' (Stack st') = Some (KStoptr stl2) ∨ l2 = Storeloc stl2)"
          then obtain i1 i2 where iDefs:" (Denvalue ev) $$ i1 = Some (type.Storage tp1, l1)
                                  ∧ (Denvalue ev) $$ i2 = Some (type.Storage tp2, l2)" by blast
          then have "(type.Storage tp1, l1) |∈| fmran (Denvalue e') ∧ (type.Storage tp2, l2) |∈| fmran (Denvalue e')" using a0
            by (metis denLink fmlookup_ran_iff)
          then show "if TypedStoSubpref stl2 stl1 tp1 then CompStoType tp1 tp2 stl1 stl2
                else if TypedStoSubpref stl1 stl2 tp2 then CompStoType tp2 tp1 stl2 stl1 else True"
            using cpNew using a0 by blast
        qed
      next
        show "balanceTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        have "envAddressesWellFormed ev" using 10(2) unfolding TypeSafe_def by simp
        then show "addressFormat (Address ev)" and "addressFormat (Sender ev)" by simp+
      next
        show "svalueTypes (Svalue ev)" using 10(2) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "lessThanTopLocs cd" using 10(2) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        have oldTC:"typeCompat (Denvalue e') (Stack st') (Memory st') (Storage st' (Address e')) cd'"
          using IH1 unfolding StateInvariant_def TypeSafe_def by blast
        show "typeCompat (Denvalue ev) (Stack st') (Memory st') (Storage st' (Address ev)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume inDen:"(t, l) |∈| fmran (Denvalue ev)"
          then have inDenN:"(t, l) |∈| fmran (Denvalue e')"
            using denLink by (meson fmlookup_ran_iff)

          show "denvalueCon (Stack st') (Memory st') (Storage st' (Address ev)) cd t l"
          proof(cases l)
            case (Stackloc x1)
            then have oldTC':"(
             case accessStore x1 (Stack st') of None ⇒ False
              | Some (KValue val) ⇒ (case t of Value typ ⇒ typeCon typ val | _ ⇒ False)
              | Some (KCDptr stloc) ⇒ (case t of Calldata struct ⇒ MCon struct cd' stloc | _ ⇒ False)
              | Some (KMemptr stloc) ⇒ (case t of type.Memory struct ⇒ MCon struct (Memory st') stloc | _ ⇒ False)
              | Some (KStoptr stloc) ⇒ (case t of type.Storage struct ⇒ SCon struct stloc (Storage st' (Address e')) | _ ⇒ False))"
              using oldTC unfolding typeCompat_def using inDenN by fastforce
            then show ?thesis
            proof(cases "accessStore x1 (Stack st')")
              case None
              then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd Stackloc by fastforce
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1')
                then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd Some Stackloc by force
              next
                case (KCDptr x2)
                then obtain struct where structDef:"t = Calldata struct ∧ MCon struct cd' x2"
                  using oldTC' unfolding typeCompat_def using inDenN sameAdd Some Stackloc by (cases t; auto)
                then show ?thesis using tsDecl KCDptr Some Stackloc sameCD by simp
              next
                case (KMemptr x3)
                then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd Some Stackloc by fastforce
              next
                case (KStoptr x4)
                then have "case t of type.Storage struct ⇒ SCon struct x4 (Storage st' (Address e')) | _ ⇒ False"
                  using oldTC' using inDenN sameAdd Some Stackloc by (auto split:option.splits type.splits stackvalue.splits denvalue.splits)
                then obtain struct where "t = type.Storage struct ∧ SCon struct x4 (Storage st' (Address e'))" by (cases t; auto)
                then show ?thesis using Some Stackloc KStoptr
                  by (simp add: sameAdd)
              qed
            qed
          next
            case (Storeloc x2)
            then show ?thesis using oldTC unfolding typeCompat_def using inDenN sameAdd
              by (metis (no_types, lifting) denvalue.simps(6) type.exhaust type.simps(17,18,19,20))
          qed
        qed
      next

        show "denvalueTypeCorrectness ev (Stack st') (Memory st')"
          unfolding denvalueTypeCorrectness_def
        proof intros
          fix t l ptr_loc sub_loc
          assume in1:"(type.Memory t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st') = Some (KMemptr ptr_loc)"
          then have in2:"(type.Memory t, Stackloc l) |∈| fmran (Denvalue e')" using denLink
            by (meson fmlookup_ran_iff)
          then show "(case t of MTArray len arr ⇒ ∀i<len. accessTypeStore (hash ptr_loc (ShowLnat i)) (Memory st') = Some arr
        | MTValue val ⇒ accessTypeStore ptr_loc (Memory st') = Some (MTValue val))"
            using IH1 unfolding StateInvariant_def TypeSafe_def denvalueTypeCorrectness_def
            using in1 in2 by blast
        qed
      next
        show "subPrefixStructuralConsistency (Memory st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      next
        show "SomeValSomeTyp (Memory st')" using IH1 unfolding StateInvariant_def TypeSafe_def by blast
      qed

    next
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address ev)) = Some (atype.Contract c) ∧
          Contract ev = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p))"
        using 10(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address ev)) = Some (atype.Contract c_fi) ∧
          Contract ev = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue ev $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue ev $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address ev)) = Some (atype.Contract c_fi)"
        using fiC atype_same "10.prems"(2,3) statement_with_gas.atype_same statement_with_gas_axioms by blast
      have fiFinal:"fullyInitialised e' (Accounts st') (Stack st')"
        using IH1 unfolding StateInvariant_def by blast
      have ptrNew:"∀t l p.
           (type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
           (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p)"
      proof (intro allI impI)
        fix t l p
        assume in1:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue ev) ∧ accessStore l (Stack st') = Some (KStoptr p)"
        have inNew:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue e')"
          using denLink in1 by (meson fmlookup_ran_iff)
        from fiFinal inNew in1 obtain t' l' where newPtr:
            "(type.Storage t', Storeloc l') |∈| fmran (Denvalue e')"
            and comp:"CompStoType t' t l' p"
          unfolding fullyInitialised_def by blast
        from newPtr obtain iid where iidDef:"Denvalue e' $$ iid = Some (type.Storage t', Storeloc l')"
          using fmlookup_ran_iff by fast
        have oldLookup:"Denvalue ev $$ iid = Some (type.Storage t', Storeloc l')"
        proof (cases "iid = id0")
          case True
          have "∀t l. Denvalue e' $$ id0 = Some (t, Storeloc l) ⟶ Denvalue ev $$ id0 = Some (t, Storeloc l)"
            using decl_env_storlocs_unchanged[OF 1(3)] by simp
          then show ?thesis using iidDef True by blast
        next
          case False
          then show ?thesis using decl_env_not_i[OF 1(3) iidDef] by blast
        qed
        have oldRan:"(type.Storage t', Storeloc l') |∈| fmran (Denvalue ev)"
          using oldLookup by (meson fmranI)
        show "∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue ev) ∧ CompStoType t' t l' p"
          using oldRan comp by blast
      qed
      show "fullyInitialised ev (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC fiPtr ptrNew typeNew by blast
    next
      fix locs t
      assume in1:"accessTypeStore locs (Memory st) = Some t"
      then have "∃v. accessStore locs (Memory st) = Some v"
        using 10(2) unfolding TypeSafe_def SomeValSomeTyp_def by blast
      then obtain i where idef:"i < Toploc (Memory st) ∧ LSubPrefL2 locs (ShowLnat i) "
        using 10(2) unfolding TypeSafe_def lessThanTopLocs_def by blast
      then have same:"accessTypeStore locs (Memory st) = accessTypeStore locs mem'"
        using tsDecl unfolding ncpOMemInDMem_def by simp
      then have "accessTypeStore locs (Memory (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = Some t"
        using in1 by simp
      then show "accessTypeStore locs (Memory st') = Some t"
        using in1 IH1 unfolding StateInvariant_def by simp
    next
      fix locs v
      assume in1:"accessStore locs (Memory st) = Some (MPointer v)"
      then obtain i where idef:"i < Toploc (Memory st) ∧ LSubPrefL2 locs (ShowLnat i) "
        using 10(2) unfolding TypeSafe_def lessThanTopLocs_def by blast
      then have same:"accessStore locs (Memory st) = accessStore locs mem'"
        using tsDecl unfolding ncpOMemInDMem_def by simp
      then have "accessStore locs (Memory (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = Some (MPointer v)"
        using in1 by simp
      then show "∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using in1 IH1 unfolding StateInvariant_def by simp
    next
      fix locs v
      assume in1:"accessStore locs (Memory st) = Some (MValue v)"
      then obtain i where idef:"i < Toploc (Memory st) ∧ LSubPrefL2 locs (ShowLnat i) "
        using 10(2) unfolding TypeSafe_def lessThanTopLocs_def by blast
      then have same:"accessStore locs (Memory st) = accessStore locs mem'"
        using tsDecl unfolding ncpOMemInDMem_def by simp
      then have "accessStore locs (Memory (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = Some (MValue v)"
        using in1 by simp
      then show "∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using in1 IH1 unfolding StateInvariant_def by simp
    next
      fix i loc
      assume in1:"i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) "
        and in2:" accessStore loc (Memory st) = None"
      then have same:"accessStore loc (Memory st) = accessStore loc mem'"
        using tsDecl unfolding ncpOMemInDMem_def by simp
      moreover have "Toploc (Memory st) ≤ Toploc mem'" using tsDecl unfolding ncpOMemInDMem_def by simp
      moreover have "i < Toploc (Memory (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) ∧ LSubPrefL2 loc (ShowLnat i)"
        using calculation in1 by auto
      ultimately have "accessStore loc (Memory (st⦇Gas := g, Stack := sck', Memory := mem'⦈)) = None ⟶
        accessStore loc (Memory st') = None" using IH1 unfolding StateInvariant_def by simp
      then show "accessStore loc (Memory st') = None" using in2 same by simp

    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')"
        using 1 IH1 unfolding StateInvariant_def using tsDecl by simp
    qed
  qed
next
  case (11 i xe val e cd st)
  show ?case
  proof(cases rule:new[OF 11(3)])
    case (1 v t g ct cn fb el cdl kl ml g' acc st'' v')
    then have a10:"assert Gas (λst. costs (NEW i xe val) e cd st < state.Gas st) st = Normal ((), st)" by simp
    let ?sgas = "st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈"
    have a20:"modify (λst. st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈) st = Normal ((), ?sgas)" by simp
    let ?newAdd = "hash_version (Address e) (ShowLnat (Contracts (Accounts ?sgas (Address e))))"
    have notSame:"(Address e) ≠ ?newAdd"
      by (metis hash_version_inequality)
    have formatAdd:"addressFormat (hash_version (Address e) (ShowLnat (Contracts (Accounts (st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈) (Address e)))))"
      unfolding addressFormat_def typeCon.simps
    proof -
      have "CHR ''.'' ∉ set (literal.explode (Address e))"
        using 11(2) unfolding TypeSafe_def addressFormat_def typeCon.simps checkAddress_def by blast
      then have "CHR ''.'' ∉ set (literal.explode (STR ''-'' + Address e))"
        by (simp add: Literal.rep_eq add_Literal_assoc)
      moreover have "CHR ''.'' ∉ set (literal.explode (ShowLnat (Contracts (Accounts (st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈) (Address e)))))"
        using ShowLNatDot by blast

      ultimately have "CHR ''.'' ∉ set
                      (literal.explode ((ShowLnat (Contracts (Accounts (st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈) (Address e))))
                      + (STR ''-'' + Address e)))"
        by (simp add: plus_literal.rep_eq)
      then show "checkAddress (hash_version (Address e) (ShowLnat (Contracts (Accounts (st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈) (Address e)))))"
        unfolding checkAddress_def hash_version_def by blast
    qed
    let ?updState = "st⦇Gas := g, Accounts := (Accounts st)((?newAdd) := ⦇Bal = ShowLint 0, Type = Some (atype.Contract i), Contracts = 0⦈),
                                                Storage := (Storage st)((?newAdd) := {$$})⦈"
    obtain uus where uusDef:"uus = ?updState" by blast
    have sameUpd:"Memory ?updState = Memory st ∧ Stack ?updState = Stack st" by auto
    have a30:"applyf (λst. hash_version (Address e) (ShowLnat (Contracts (Accounts st (Address e))))) (?sgas)
                    = Normal (hash_version (Address e) (ShowLnat (Contracts (Accounts ?sgas (Address e)))) , (?sgas))" using 1 by auto
    have a40:" assert Err (λst. Type (Accounts st ?newAdd) = None) (?sgas)
                          = Normal ((), ?sgas)" using 1 by simp
    have a50:"toState (local.expr val e cd) (?sgas) = Normal ((KValue v, Value t), st⦇Gas := g⦈)" using 1 by auto
    have a60:"(case (KValue v, Value t) of (KValue v, Value t) ⇒ return (v, t) | (KValue v, _) ⇒ throw Err
                  | (_, b) ⇒ throw Err) (st⦇Gas := g⦈) = Normal ((v,t), st⦇Gas := g⦈)" using 1 by simp
    have a65:"option Err (λ_. convert t (TUInt b256) v) (st⦇Gas := g⦈) = Normal (v', st⦇Gas := g⦈)" using 1 by simp
    have a70:"option Err (λ_. ep $$ i) (st⦇Gas := g⦈) = Normal ((ct, cn, fb), st⦇Gas := g⦈)" using 1 by simp
    obtain folded where a80: "folded = ffold_init ct (emptyEnv (?newAdd) i (Address e) v') (fmdom ct)"
      using 1 by blast
    have a90:"toState (local.load True (fst cn) xe folded emptyTypedStore emptyStore emptyTypedStore e cd) ?updState = Normal ((el,
        cdl, kl, ml), ?updState⦇Gas := g'⦈)"
      using 1 a80 by simp


    have a110:" option Err (λst. transfer (Address e) ?newAdd v' (Accounts st)) (?updState⦇Gas:=g'⦈) = Normal (acc, ?updState⦇Gas:=g'⦈)"
      using 1 by auto
    have a120:"applyf (λst. (Stack st, Memory st)) (?updState⦇Gas:=g'⦈) = Normal ((Stack st, Memory st), ?updState⦇Gas:=g'⦈)"
      by simp
    have a130:"modify (λst'. st'⦇Accounts := acc, Stack := kl, Memory := ml⦈) (?updState ⦇Gas:=g'⦈)
              = Normal ((), ?updState⦇Gas:=g', Accounts := acc, Stack := kl, Memory := ml⦈)"
      by simp

    have BtUus:"balanceTypes (Accounts uus)"
      unfolding balanceTypes_def
    proof intros
      fix adv
      show "typeCon (TUInt b256) (Bal (Accounts uus adv)) "
      proof(cases "adv = ?newAdd")
        case True
        then have " (Bal (Accounts uus adv)) = (ShowLint 0)" using uusDef by simp
        then show ?thesis unfolding typeCon.simps checkUInt_def
          using Read_ShowL_id by presburger
      next
        case False
        then have "(Bal (Accounts uus adv)) = (Bal (Accounts st adv))" using uusDef by simp
        then show ?thesis using 11(2) unfolding TypeSafe_def balanceTypes_def by simp
      qed
    qed

    have svv':"svalueTypes v'" unfolding svalueTypes_def
      using 1(4,3) 11(2) 11(4)
      using exprTypeconInduct(3)[of ]
        typeSafeConvert[of t v "(TUInt b256)"] convertSame by fastforce

    have btACC:"balanceTypes acc" unfolding balanceTypes_def
    proof -
      have old:"balanceTypes (Accounts (uus))" using BtUus by simp
      have accExp:"(case subBalance (Address e) v' (Accounts (uus)) of None ⇒ None | Some x ⇒ addBalance ?newAdd v' x) = Some acc"
        using 1(7) uusDef unfolding transfer_def by simp
      have v'TC: "typeCon (TUInt b256) v'"
        using svv' unfolding svalueTypes_def by blast

      show "∀adv'. typeCon (TUInt b256) (Bal (acc adv')) "
      proof(cases "?newAdd = (Address e)")
        case same:True
        then show ?thesis using notSame by simp
      next
        case notSame:False
        show ?thesis
        proof
          fix adv'
          show "typeCon (TUInt b256) (Bal (acc adv')) "
          proof(cases "adv' = (Address e)")
            case True

            then have tco:"typeCon (TUInt b256) (Bal ((Accounts uus) adv'))"
              using True old accExp balanceTypes_def by blast
            have "Bal (acc (Address e)) = ShowLint (ReadLint (Bal (Accounts (uus) (Address e))) - (ReadLint v'))"
              using transfer_subRead[OF _ _ v'TC ] 1(7) uusDef True notSame tco by auto
            then show ?thesis using True
              using "1"(7,3) Read_ShowL_id tco checkUInt_def transfer_val1 transfer_val2
              by (metis transfer_Bal_typeCon[unfolded transfer_def, OF accExp] old[unfolded balanceTypes_def])

          next
            case False
            then show ?thesis
            proof(cases "adv' = ?newAdd")
              case True
              then have tco:"typeCon (TUInt b256) (Bal ((Accounts uus) ?newAdd))"
                using True old accExp balanceTypes_def by simp
              then have "Bal (acc ?newAdd) = (ShowLint ((ReadLint (Bal (Accounts (uus) ?newAdd))) + (ReadLint v')))"
                using transfer_addRead[OF 1(7)_ v'TC ] 1(3) notSame uusDef by auto
              moreover have "typeCon (TUInt b256) (ShowLint ((ReadLint (Bal (Accounts (uus) ?newAdd))) + (ReadLint v')))"
                using "1"(7,3) Read_ShowL_id tco checkUInt_def transfer_val1 transfer_val2 transfer_val3
                using transfer_Bal_typeCon[unfolded transfer_def, OF accExp] old calculation unfolding balanceTypes_def by metis
              ultimately show ?thesis using True by simp
            next
              case f2:False
              show ?thesis using transfer_eq[OF 1(7) False f2] old
                using balanceTypes_def uusDef by simp
            qed
          qed
        qed
      qed
    qed

    obtain ii :: environment where iiDef: "i = Contract ii" using 1
      by (metis Environment.select_convs(2))
    have epA:"ep $$ Contract ii = Some (ct, cn, fb)" using 1 iiDef by blast
    have atUus:"AddressTypes (Accounts uus)"
      unfolding AddressTypes_def
    proof intros
      fix adv
      show "case Type (Accounts uus adv) of None ⇒ True | Some EOA ⇒ True | Some (atype.Contract c) ⇒ addressFormat adv ∧ c |∈| fmdom ep "
      proof(cases "Type (Accounts uus adv)")
        case None
        then show ?thesis by simp
      next
        case (Some a)
        then show ?thesis
        proof(cases a)
          case EOA
          then show ?thesis using Some by simp
        next
          case (Contract x2)
          then show ?thesis
          proof(cases "adv = ?newAdd")
            case True
            then have "Type (Accounts uus adv) = Some (atype.Contract i)" using uusDef by auto
            then have "i = x2" using Some Contract by simp
            moreover have "ep $$ i = Some (ct, cn, fb)" using epA iiDef by simp
            moreover have "addressFormat adv" using iiDef epA formatAdd True by simp
            ultimately show ?thesis using Some Contract
              by (metis Option.option.simps(5) atype.simps(5) fmlookup_dom_iff)
          next
            case False
            then have "(Accounts uus adv) = Accounts st adv" using uusDef by simp
            then show ?thesis using 11(2) unfolding TypeSafe_def AddressTypes_def using Some Contract
              by metis
          qed
        qed
      qed
    qed
    have scUus:"safeContract (Accounts uus) (Storage uus)"
      unfolding safeContract_def
    proof intros
      fix e' ct' dud' i' tp
      assume in1:"Type (Accounts uus (Address (e'::environment))) = Some (atype.Contract (Contract e')) ∧
                 ep $$ Contract (e'::environment) = Some (ct', dud') ∧
                 ct' $$ i' = Some (Var tp)"
      show "SCon tp i' (Storage uus (Address e'))"
      proof(cases "Address e' = ?newAdd")
        case True
        then have "(Storage uus (Address e')) = {$$}" using uusDef by simp
        then show ?thesis
        proof(induction tp arbitrary:i')
          case (STArray x1 tp)
          then show ?case by simp
        next
          case (STMap x1 tp)
          then show ?case by simp
        next
          case (STValue x)
          have "typeCon x (ival x)" by (simp add: ivalTypeCon)
          then show ?case unfolding SCon.simps accessStorage_def using STValue by simp
        qed
      next
        case False
        then have "Storage uus (Address e') = Storage st (Address e')"
          using uusDef by simp
        moreover have "Type (Accounts st (Address e')) = Some (atype.Contract (Contract e'))"
          using False in1 uusDef by simp
        moreover have "ep $$ Contract (e'::environment) = Some (ct', dud')"
          using in1 by simp
        moreover have "ct' $$ i' = Some (Var tp)"
          using in1 by simp
        moreover have "safeContract (Accounts st) (Storage st)"
          using 11(2) unfolding TypeSafe_def by simp
        ultimately show ?thesis
          unfolding safeContract_def
          by presburger
      qed
    qed
    have cohUus:"Type (Accounts uus ?newAdd) = Some (atype.Contract (Contract ii))"
      using uusDef iiDef by simp
    have TSInit:"∀x y. TypeSafe folded (Accounts uus)
                  emptyStore emptyTypedStore (Storage uus) emptyTypedStore ∧
       (Denvalue folded $$ x = Some y ⟶ snd y = Storeloc x) ∧ (Denvalue folded $$ x = Some y ⟶
           (∃t1. ct $$ x = Some (Var t1) ∧ fst y = type.Storage t1))"
    proof -
      have "safeContract (Accounts uus) (Storage uus)" using scUus by simp
      moreover have "balanceTypes (Accounts uus)" using BtUus by simp
      moreover have "svalueTypes v'"
      proof -
        have "typeCon t (extractValueType (KValue v))"
          using 1(4,3) 11(2) 11(4)
          using exprTypeconInduct(3)[of val e cd "(st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈)" "(state.Gas
              (st⦇Gas := state.Gas st - costs (NEW i xe val) e cd st⦈))" "KValue v" "Value t" g]
            typeSafeConvert[of t v "(TUInt b256)"] convertSame by simp
        then show ?thesis unfolding svalueTypes_def using 1(3) a65
          by (metis "1"(4) convertSame extractValueType.simps(1) typeSafeConvert)
      qed
      moreover have "lessThanTopLocs emptyStore" using emptyTopLocs by simp
      moreover have "ep $$ Contract ii = Some (ct, cn, fb)" using epA by simp
      moreover have "addressFormat (?newAdd)" using formatAdd by simp
      moreover have "addressFormat (Address e)" using 11(2) unfolding TypeSafe_def by simp
      moreover have "AddressTypes (Accounts uus)" using atUus by simp
      moreover have sp:"subPrefixStructuralConsistency emptyTypedStore" unfolding subPrefixStructuralConsistency_def accessTypeStore_def emptyTypedStore_def by simp
      moreover have "SomeValSomeTyp emptyTypedStore" unfolding SomeValSomeTyp_def accessTypeStore_def accessStore_def emptyTypedStore_def by auto
      moreover have "Type (Accounts uus ?newAdd) = Some (atype.Contract (Contract ii))" using cohUus by simp

      ultimately show "∀x y. TypeSafe folded (Accounts uus)
                  emptyStore emptyTypedStore (Storage uus) emptyTypedStore ∧
       (Denvalue folded $$ x = Some y ⟶ snd y = Storeloc x) ∧ (Denvalue folded $$ x = Some y ⟶
           (∃t1. ct $$ x = Some (Var t1) ∧ fst y = type.Storage t1))"
        using a80 unfolding ffold_init_def
        using ffoldInitTypeSafe[of "Accounts uus" "Storage uus" v' emptyTypedStore ii ct "(cn, fb)" ?newAdd "Address e" "fmdom ct"]
        using iiDef typedEmptyTopLocs by blast
    qed
    have a140:"TypeSafe el (Accounts (uus⦇Gas:=g', Accounts := acc, Stack := kl, Memory := ml⦈))
                            (Stack (uus⦇Gas:=g', Accounts := acc, Stack := kl, Memory :=ml⦈))
                            (Memory (uus⦇Gas:=g', Accounts := acc, Stack := kl, Memory := ml⦈))
                            (Storage (uus⦇Gas:=g', Accounts := acc, Stack := kl, Memory := ml⦈))
                            cdl ∧ fullyInitialised el acc kl"
    proof -

      have aa1:"load True (fst cn) xe folded emptyTypedStore emptyStore emptyTypedStore e cd ?updState g =
    Normal ((el, cdl, kl, ml), g')" using 1(6) a80 unfolding ffold_init_def by simp
      moreover have tsE:"TypeSafe e (Accounts uus) (Stack uus) (Memory uus) (Storage uus) cd"
        unfolding TypeSafe_def
      proof intros
        show "AddressTypes (Accounts uus)" using atUus by simp
      next
        show "safeContract (Accounts uus) (Storage uus)" using scUus by simp
      next
        show "unique_locations (Denvalue e)" using 11(2) unfolding TypeSafe_def by simp
      next

        show "compPointers (Stack uus) (Denvalue e)"
          using 11(2) unfolding TypeSafe_def
          using sameUpd uusDef by argo
      next
        show "balanceTypes (Accounts uus)" using BtUus by simp
      next
        have "envAddressesWellFormed e" using 11(2) sameUpd uusDef unfolding TypeSafe_def by simp
        then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
      next
        show "svalueTypes (Svalue e)" using 11(2) sameUpd uusDef unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack uus)" using 11(2) sameUpd uusDef unfolding TypeSafe_def by auto
      next
        show "lessThanTopLocs cd" using 11(2) sameUpd uusDef unfolding TypeSafe_def by auto
      next
        show "lessThanTopLocs (Memory uus)" using 11(2) sameUpd uusDef unfolding TypeSafe_def by auto
      next
        have tcOld:"typeCompat (Denvalue e) (Stack st) (Memory st) (Storage st (Address e)) cd"
          using 11(2) unfolding TypeSafe_def by blast
        show "typeCompat (Denvalue e) (Stack uus) (Memory uus) (Storage uus (Address e)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume inDen:"(t, l) |∈| fmran (Denvalue e)"

          show "denvalueCon (Stack uus) (Memory uus) (Storage uus (Address e)) cd t l"
          proof(cases l)
            case (Stackloc x1)
            have tcOld':"case accessStore x1 (Stack st) of None ⇒ False
                         | Some (KValue val) ⇒ (case t of Value typ ⇒ typeCon typ val | _ ⇒ False)
                         | Some (KCDptr stloc) ⇒ (case t of Calldata struct ⇒ MCon struct cd stloc | _ ⇒ False)
                         | Some (KMemptr stloc) ⇒ (case t of type.Memory struct ⇒ MCon struct (Memory st) stloc | _ ⇒ False)
                         | Some (KStoptr stloc) ⇒ (case t of type.Storage struct ⇒ SCon struct stloc (Storage st (Address e)) | _ ⇒ False)"
              using sameUpd Stackloc inDen tcOld unfolding typeCompat_def by force
            then show ?thesis
            proof(cases "accessStore x1 (Stack uus)")
              case None
              then show ?thesis using tcOld' sameUpd uusDef by simp
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1')
                then show ?thesis using tcOld' sameUpd uusDef Some Stackloc by simp
              next
                case (KCDptr x2)

                then show ?thesis using tcOld' sameUpd uusDef Some Stackloc by simp
              next
                case (KMemptr x3)
                then show ?thesis using tcOld' sameUpd uusDef Some Stackloc by (cases t; fastforce)
              next
                case (KStoptr x4)

                then show ?thesis
                proof(cases "Address e = ?newAdd")
                  case True
                  then have empt:"Storage uus (Address e) = {$$}" using uusDef by simp
                  then obtain struct where struct_def:"t = type.Storage struct"
                    using tcOld' sameUpd uusDef Some Stackloc KStoptr by (cases t; fastforce)
                  have "SCon struct x4 (Storage uus (Address e))"
                  proof(induction struct arbitrary:x4)
                    case (STArray x1 tp)
                    then show ?case by simp
                  next
                    case (STMap x1 tp)
                    then show ?case by simp
                  next
                    case (STValue x)
                    have "typeCon x (ival x)" by (simp add: ivalTypeCon)
                    then show ?case unfolding SCon.simps accessStorage_def using STValue empt by simp
                  qed
                  then show ?thesis using tcOld' sameUpd uusDef Some Stackloc KStoptr struct_def by simp
                next
                  case False
                  then have "Storage uus (Address e) = Storage st (Address e)" using uusDef by simp
                  then show ?thesis using tcOld' sameUpd uusDef Some Stackloc KStoptr by (cases t; fastforce)
                qed
              qed
            qed
          next
            case (Storeloc x2)
            then show ?thesis
            proof(cases "Address e = ?newAdd")
              case True
              then have empt:"Storage uus (Address e) = {$$}" using uusDef by simp
              then obtain struct where struct_def:"t = type.Storage struct"
                using sameUpd uusDef Storeloc tcOld inDen unfolding typeCompat_def by (cases t; fastforce)
              have "SCon struct x2 (Storage uus (Address e))"
              proof(induction struct arbitrary:x2)
                case (STArray x1 tp)
                then show ?case by simp
              next
                case (STMap x1 tp)
                then show ?case by simp
              next
                case (STValue x)
                have "typeCon x (ival x)" by (simp add: ivalTypeCon)
                then show ?case unfolding SCon.simps accessStorage_def using STValue empt by simp
              qed
              then show ?thesis using tcOld inDen unfolding typeCompat_def using sameUpd uusDef Storeloc struct_def by simp
            next
              case False
              then have "Storage uus (Address e) = Storage st (Address e)" using uusDef by simp
              then show ?thesis using tcOld inDen unfolding typeCompat_def using sameUpd uusDef Storeloc by (cases t; fastforce)
            qed
          qed
        qed
      next
        show "denvalueTypeCorrectness e (Stack uus) (Memory uus)" using uusDef using 11(2) unfolding TypeSafe_def by simp
      next
        show "subPrefixStructuralConsistency (Memory uus)" using uusDef using 11(2) unfolding TypeSafe_def by simp
      next
        show "SomeValSomeTyp (Memory uus)" using uusDef using 11(2) unfolding TypeSafe_def by simp
      qed

      moreover have "TypeSafe folded (Accounts uus) emptyStore emptyTypedStore (Storage uus) emptyTypedStore" using TSInit
        by blast

      moreover have a6:"fullyInitialised e (Accounts uus) (Stack uus)"
      proof -
        have fiOld:"
          ∃c ct dud.
            Type (Accounts st (Address e)) = Some (atype.Contract c) ∧
            Contract e = c ∧
            ep $$ c = Some (ct, dud) ∧
            (∀id v. (ct $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
            (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
            (∀t l p.
                (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
                (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p))"
          using 11(4) unfolding fullyInitialised_def by blast
        then obtain c_fi ct_fi dud_fi where fiC:"
            Type (Accounts st (Address e)) = Some (atype.Contract c_fi) ∧
            Contract e = c_fi ∧
            ep $$ c_fi = Some (ct_fi, dud_fi) ∧
            (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
            (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
          and fiPtr:"∀t l p.
                (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
                (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
          by blast
        have typeNew:"Type (Accounts uus (Address e)) = Some (atype.Contract c_fi)"
          using fiC uusDef notSame by simp
        have ptrNew:"∀t l p.
            (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack uus) = Some (KStoptr p) ⟶
            (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
          using fiPtr sameUpd uusDef by simp
        show ?thesis
          unfolding fullyInitialised_def using fiC typeNew ptrNew by blast
      qed

      have contractL:"Contract folded = Contract ii" using a80 iiDef by simp

      have ctd:"ep $$ Contract folded = Some (ct, cn, fb)" using a50 contractL 1 a10 iiDef by metis
      then have fi2:"∀id v. (Denvalue folded $$ id = Some (type.Storage v, Storeloc id)) = (ct $$ id = Some (Var v))"
        using a80 unfolding ffold_init_def using ffoldInit_var_storage_mapping_eq[of ct ?newAdd i "(Address e)" v' folded] by auto
      moreover have "fullyInitialised e (Accounts ?updState) (Stack ?updState)" using a6 uusDef by blast
      moreover have fiFolded:"fullyInitialised folded (Accounts uus) emptyStore"
      proof -
        obtain c_fi where cfi:
          "Type (Accounts uus (Address folded)) = Some (atype.Contract c_fi) ∧ Contract folded = c_fi"
          using a80 11(4) uusDef unfolding fullyInitialised_def by simp
        obtain dud_fi where epfi:"ep $$ Contract folded = Some (ct, dud_fi)"
          using ctd by blast
        have mapfi:"∀id v. (ct $$ id = Some (Var v)) = (Denvalue folded $$ id = Some (type.Storage v, Storeloc id))"
          using fi2 by blast
        have locfi:"∀id v loc. Denvalue folded $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
        proof (intro allI impI)
          fix id v loc
          assume h:"Denvalue folded $$ id = Some (type.Storage v, Storeloc loc)"
          have hloc:"Denvalue folded $$ id = Some (type.Storage v, Storeloc loc) ⟶ snd (type.Storage v, Storeloc loc) = Storeloc id"
            using TSInit by blast
          then show "id = loc" using h by simp
        qed
        have ptrfi:"∀t l p.
            (type.Storage t, Stackloc l) |∈| fmran (Denvalue folded) ∧ accessStore l emptyStore = Some (KStoptr p) ⟶
            (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue folded) ∧ CompStoType t' t l' p)"
          by (simp add: accessStore_def emptyStore_def)
        show ?thesis
          unfolding fullyInitialised_def using cfi epfi mapfi locfi ptrfi by blast
      qed

      ultimately have exprTS:" TypeSafe el (Accounts ?updState) kl ml (Storage ?updState) cdl ∧
    fullyInitialised el (Accounts ?updState) kl ∧ fullyInitialised el
     (Accounts ?updState) kl"
        using exprTypeconInduct(4)[of True "fst cn" xe folded emptyTypedStore emptyStore
            emptyTypedStore e cd ?updState g el cdl kl ml g'] uusDef by blast

      have cc1:"TypeSafe el (acc) (kl) (ml) (Storage (uus)) cdl"
        unfolding TypeSafe_def
      proof intros
        show "AddressTypes acc" unfolding AddressTypes_def using tsE unfolding TypeSafe_def AddressTypes_def using 1(7) transfer_type_same uusDef by simp
      next
        have trUus:"transfer (Address e) ?newAdd v' (Accounts uus) = Some acc"
          using 1(7) uusDef by simp
        have scUus':"safeContract (Accounts uus) (Storage uus)"
          using tsE unfolding TypeSafe_def by simp
        show "safeContract acc (state.Storage uus)"
        proof (unfold safeContract_def, intro allI impI)
          fix env ct' dud' i' tp
          assume in1:"Type (acc (Address (env::environment))) = Some (atype.Contract (Contract env)) ∧
                     ep $$ Contract env = Some (ct', dud') ∧
                     ct' $$ i' = Some (Var tp)"
          have typeOld:"Type (Accounts uus (Address env)) = Some (atype.Contract (Contract env))"
            using in1 transfer_type_same[OF trUus, of "Address env"] by simp
          show "SCon tp i' (state.Storage uus (Address env))"
            using scUus' in1 typeOld unfolding safeContract_def by blast
        qed
      next
        show "unique_locations (Denvalue el)" using exprTS unfolding TypeSafe_def by blast
      next
        show "compPointers kl (Denvalue el)" using exprTS unfolding TypeSafe_def by blast
      next
        show "balanceTypes acc" using btACC by auto
      next
        have "envAddressesWellFormed el" using exprTS unfolding TypeSafe_def by blast
        then show "addressFormat (Address el)" and "addressFormat (Sender el)" by simp+
      next
        show "svalueTypes (Svalue el)" using exprTS unfolding TypeSafe_def by blast
      next
        show " lessThanTopLocs kl" using exprTS unfolding TypeSafe_def by blast
      next
        show " lessThanTopLocs cdl" using exprTS unfolding TypeSafe_def by blast
      next
        show "lessThanTopLocs ml" using exprTS unfolding TypeSafe_def by blast
      next
        show "typeCompat (Denvalue el) kl ml (Storage uus (Address el)) cdl" using uusDef exprTS unfolding TypeSafe_def by blast
      next
        show "denvalueTypeCorrectness el kl ml" using TypeSafe_def exprTS by blast
      next
        show "subPrefixStructuralConsistency ml" using TypeSafe_def exprTS by blast
      next
        show "SomeValSomeTyp ml" using TypeSafe_def exprTS by blast
      qed

      have elFolded:"Address el = Address folded ∧ Contract el = Contract folded" using 1 a80
        using msel_ssel_expr_load_rexp_gas(4) aa1 by blast
      have cc2:"fullyInitialised el acc kl"
      proof -
        have fiOld:"
          ∃c ct dud.
            Type (Accounts ?updState (Address el)) = Some (atype.Contract c) ∧
            Contract el = c ∧
            ep $$ c = Some (ct, dud) ∧
            (∀id v. (ct $$ id = Some (Var v)) = (Denvalue el $$ id = Some (type.Storage v, Storeloc id))) ∧
            (∀id v loc. Denvalue el $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
            (∀t l p.
                (type.Storage t, Stackloc l) |∈| fmran (Denvalue el) ∧ accessStore l kl = Some (KStoptr p) ⟶
                (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue el) ∧ CompStoType t' t l' p))"
          using exprTS unfolding fullyInitialised_def by blast
        then obtain c_fi ct_fi dud_fi where fiC:"
            Type (Accounts ?updState (Address el)) = Some (atype.Contract c_fi) ∧
            Contract el = c_fi ∧
            ep $$ c_fi = Some (ct_fi, dud_fi) ∧
            (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue el $$ id = Some (type.Storage v, Storeloc id))) ∧
            (∀id v loc. Denvalue el $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
          and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue el) ∧ accessStore l kl = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue el) ∧ CompStoType t' t l' p)"
          by blast
        have typeNew:"Type (acc (Address el)) = Some (atype.Contract c_fi)"
          using fiC 1(7) transfer_type_same uusDef by simp
        show ?thesis
          unfolding fullyInitialised_def using fiC fiPtr typeNew by blast
      qed

      then show "TypeSafe el (Accounts (uus⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈)) (Stack
          (uus⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈))
     (Memory (uus⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈)) (Storage (uus⦇Gas:=g',Accounts :=
         acc, Stack := kl, Memory := ml⦈)) cdl
∧ fullyInitialised el acc kl"
        using cc1 by auto
    qed
    have a140':"TypeSafe el (Accounts (?updState⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈))
        (Stack (?updState⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈))
   (Memory (?updState⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈)) (Storage
       (?updState⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈)) cdl"
      using a140 uusDef by auto
    have "st⦇Gas := g', Accounts := (Accounts st)((?newAdd) := ⦇Bal = ShowLint 0, Type = Some (atype.Contract i), Contracts = 0⦈),
                                                Storage := (Storage st)((?newAdd) := {$$})⦈
= (?updState⦇Gas := g'⦈)" by simp
    then have a18Exp:"local.stmt (snd cn) el cdl
     (?updState⦇Gas := g', Accounts := acc, Stack := kl, Memory := ml⦈) = Normal ((), st'')" using 1(8) by argo
    have a150:"fullyInitialised el acc kl" using a140 by simp


    have IH1:" StateInvariant el (?updState⦇Gas:=g',Accounts := acc, Stack := kl, Memory := ml⦈) st'' cdl"
      using 11(1)[OF a10 a20 a30 a40 a50 a60 _ a65 a70 _ _ _ a80 a90 _ _ _ a110 a120 _ a130 a140' a18Exp] a30 1(8) a150 by simp

    have st'Final:"st' = incrementAccountContracts (Address e) (st''⦇Stack := Stack st, Memory := Memory st⦈)" using 1 by blast

    then have sameStores:"Memory st = Memory st' ∧ Stack st = Stack st'" using 1 by simp
    show ?thesis unfolding StateInvariant_def
    proof intros
      show "TypeSafe e (Accounts st') (Stack st') (Memory st') (Storage st') cd" unfolding TypeSafe_def
      proof intros
        have old:"AddressTypes (Accounts st'')" using IH1 unfolding StateInvariant_def TypeSafe_def by auto
        show "AddressTypes (Accounts st')" using old unfolding AddressTypes_def using st'Final by fastforce

      next
        have scOld:"safeContract (Accounts st'') (Storage st'')"
          using IH1 unfolding StateInvariant_def TypeSafe_def by blast
        show "safeContract (Accounts st') (Storage st')"
        proof (unfold safeContract_def, intro allI impI)
          fix env ct' dud' i' tp
          assume in1:"Type (Accounts st' (Address (env::environment))) = Some (atype.Contract (Contract env)) ∧
                     ep $$ Contract env = Some (ct', dud') ∧ ct' $$ i' = Some (Var tp)"
          have typeOld:"Type (Accounts st'' (Address env)) = Some (atype.Contract (Contract env))"
            using in1 st'Final by simp
          have stoEq:"Storage st' (Address env) = Storage st'' (Address env)"
            using st'Final by simp
          have "SCon tp i' (Storage st'' (Address env))"
            using scOld in1 typeOld unfolding safeContract_def by blast
          then show "SCon tp i' (Storage st' (Address env))"
            using stoEq by simp
        qed
      next
        show "unique_locations (Denvalue e)" using 11(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "compPointers (Stack st') (Denvalue e)" using 11(2) st'Final unfolding StateInvariant_def TypeSafe_def by simp
      next
        show "balanceTypes (Accounts st')" using IH1 unfolding StateInvariant_def TypeSafe_def balanceTypes_def using st'Final sameStores by fastforce
      next
        have "envAddressesWellFormed e" using "11.prems"(1) unfolding TypeSafe_def by simp
        then show "addressFormat (Address e)" and "addressFormat (Sender e)" by simp+
      next
        show "svalueTypes (Svalue e)" using "11.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Stack st')" using 11(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "lessThanTopLocs cd" using "11.prems"(1) unfolding TypeSafe_def by simp
      next
        show "lessThanTopLocs (Memory st')" using 11(2) unfolding TypeSafe_def using sameStores by simp
      next
        show "typeCompat (Denvalue e) (Stack st') (Memory st') (Storage st' (Address e)) cd"
          unfolding typeCompat_def
        proof intros
          fix t l
          assume inDen:"(t, l) |∈| fmran (Denvalue e)"

          have acc:"(∀adv. case Type (Accounts st adv) of None ⇒ True | Some EOA ⇒ True | Some
              (atype.Contract c) ⇒ addressFormat adv ∧ c |∈| fmdom ep)"
            using 11(2) unfolding TypeSafe_def AddressTypes_def by blast
          obtain cOld where cOldDef:"Type (Accounts st (Address e)) = Some (atype.Contract cOld) ∧ Contract e = cOld"
            using 11(4) unfolding fullyInitialised_def by simp
          then obtain ctO dudO where ctoDef: "ep $$ Contract e = Some (ctO, dudO)" using acc
            using "11.prems"(3) typesafe_base.fullyInitialised_def typesafe_base_axioms by blast
          have storageFinal:"Storage st' = Storage st''" using 1 by auto
          have scSt'':"safeContract (Accounts st'') (Storage st'')"
            using IH1 unfolding StateInvariant_def TypeSafe_def by blast
          have trUus:"transfer (Address e) ?newAdd v' (Accounts uus) = Some acc"
            using 1(7) uusDef by simp
          have typeStE:"Type (Accounts st (Address e)) = Some (atype.Contract (Contract e))"
            using cOldDef by simp
          have typeUusE:"Type (Accounts uus (Address e)) = Some (atype.Contract (Contract e))"
            using typeStE notSame uusDef by simp
          have typeAccE:"Type (acc (Address e)) = Some (atype.Contract (Contract e))"
            using transfer_type_same[OF trUus, of "Address e"] typeUusE by simp
          let ?stInit = "?updState⦇Gas := g', Accounts := acc, Stack := kl, Memory := ml⦈"
          have typeInitE:"Type (Accounts ?stInit (Address e))
                        = Some (atype.Contract (Contract e))"
            using typeAccE by simp
          have typeSt''E:"Type (Accounts st'' (Address e)) = Some (atype.Contract (Contract e))"
          proof (rule atype_same)
            show "local.stmt (snd cn) el cdl ?stInit = Normal ((), st'')"
              using 1(8)
              using a18Exp by argo
            show "Type (Accounts ?stInit (Address e)) = Some (atype.Contract (Contract e))"
              using typeInitE .
          qed

          show "denvalueCon (Stack st') (Memory st') (Storage st' (Address e)) cd t l"
          proof(cases l)
            case (Stackloc x1)

            then show ?thesis
            proof(cases "accessStore x1 (Stack st')")
              case None
              then show ?thesis using 11(2) unfolding TypeSafe_def typeCompat_def using inDen sameStores Stackloc by fastforce
            next
              case (Some a)
              then show ?thesis
              proof(cases a)
                case (KValue x1')
                then show ?thesis using 11(2) unfolding TypeSafe_def typeCompat_def using inDen sameStores Stackloc Some by fastforce
              next
                case (KCDptr x2)
                then show ?thesis using 11(2) unfolding TypeSafe_def typeCompat_def using inDen sameStores Stackloc Some by fastforce
              next
                case (KMemptr x3)
                then show ?thesis using 11(2) unfolding TypeSafe_def typeCompat_def using inDen sameStores Stackloc Some by (cases t; fastforce)
              next
                case (KStoptr x4)
                then have accO:"accessStore x1 (Stack st) = Some( KStoptr x4)"
                  using Some 1 by auto
                then obtain struct where structDef: "t = type.Storage struct"
                  using 11(2) unfolding TypeSafe_def typeCompat_def using inDen Some KStoptr Stackloc
                  by (cases t) fastforce+
                obtain tprnt lprnt where tprntDef: "((type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e) ∧ CompStoType tprnt struct lprnt x4)"
                  using 11(4) using accO inDen unfolding fullyInitialised_def
                  using Stackloc structDef by blast
                then have "SCon tprnt lprnt (Storage st'' (Address e))"
                proof -
                  from tprntDef have inRan: "(type.Storage tprnt, Storeloc lprnt) |∈| fmran (Denvalue e)" by simp
                  then obtain k where kDef: "Denvalue e $$ k = Some (type.Storage tprnt, Storeloc lprnt)"
                    using fmlookup_ran_iff by auto
                  from 11(4) obtain ct_x dud_x where
                    epX: "ep $$ (Contract e) = Some (ct_x, dud_x)" and
                    bijX: "∀id v. ct_x $$ id = Some (Var v) ⟷ Denvalue e $$ id = Some (type.Storage v, Storeloc id)" and
                    uniqX: "∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
                    unfolding fullyInitialised_def by blast
                  from kDef uniqX have "k = lprnt" by auto
                  then have "Denvalue e $$ lprnt = Some (type.Storage tprnt, Storeloc lprnt)" using kDef by simp
                  then have "ct_x $$ lprnt = Some (Var tprnt)" using bijX by auto
                  moreover from epX ctoDef have "ct_x = ctO" by auto
                  ultimately have "ctO $$ lprnt = Some (Var tprnt)" by simp
                  then show ?thesis
                    using ctoDef scSt'' typeSt''E unfolding safeContract_def by blast
                qed

                then have "SCon struct x4 (Storage st'' (Address e))"
                  using tprntDef SCon_imps_sublocs by blast
                then show ?thesis using Stackloc Some KStoptr structDef storageFinal by simp
              qed
            qed
          next
            case (Storeloc x2)
            then obtain struct where structDef: "t = type.Storage struct"
              using 11(2) inDen unfolding TypeSafe_def typeCompat_def by (cases t,fastforce+)

            then have "SCon struct x2 (Storage st'' (Address e))"
            proof -
              from inDen Storeloc structDef have inRan: "(type.Storage struct, Storeloc x2) |∈| fmran (Denvalue e)" by simp
              then obtain k where kDef: "Denvalue e $$ k = Some (type.Storage struct, Storeloc x2)"
                using fmlookup_ran_iff by auto
              from 11(4) obtain ct_x dud_x where
                epX: "ep $$ (Contract e) = Some (ct_x, dud_x)" and
                bijX: "∀id v. ct_x $$ id = Some (Var v) ⟷ Denvalue e $$ id = Some (type.Storage v, Storeloc id)" and
                uniqX: "∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc"
                unfolding fullyInitialised_def by blast
              from kDef uniqX have "k = x2" by auto
              then have "Denvalue e $$ x2 = Some (type.Storage struct, Storeloc x2)" using kDef by simp
              then have "ct_x $$ x2 = Some (Var struct)" using bijX by auto
              moreover from epX ctoDef have "ct_x = ctO" by auto
              ultimately have "ctO $$ x2 = Some (Var struct)" by simp
              then show ?thesis
                using ctoDef scSt'' typeSt''E unfolding safeContract_def by blast
            qed
            then show ?thesis using Storeloc structDef
              by (simp add: storageFinal)
          qed
        qed
      next
        show "denvalueTypeCorrectness e (Stack st') (Memory st')" using 1(9) 11(2) unfolding TypeSafe_def by simp
      next
        show "subPrefixStructuralConsistency (Memory st')" using 1(9) 11(2) unfolding TypeSafe_def by simp
      next
        show "SomeValSomeTyp (Memory st')" using 1(9) 11(2) unfolding TypeSafe_def by simp
      qed
    next
      have fiOld:"
        ∃c ct dud.
          Type (Accounts st (Address e)) = Some (atype.Contract c) ∧
          Contract e = c ∧
          ep $$ c = Some (ct, dud) ∧
          (∀id v. (ct $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc) ∧
          (∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p))"
        using 11(4) unfolding fullyInitialised_def by blast
      then obtain c_fi ct_fi dud_fi where fiC:"
          Type (Accounts st (Address e)) = Some (atype.Contract c_fi) ∧
          Contract e = c_fi ∧
          ep $$ c_fi = Some (ct_fi, dud_fi) ∧
          (∀id v. (ct_fi $$ id = Some (Var v)) = (Denvalue e $$ id = Some (type.Storage v, Storeloc id))) ∧
          (∀id v loc. Denvalue e $$ id = Some (type.Storage v, Storeloc loc) ⟶ id = loc)"
        and fiPtr:"∀t l p.
              (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st) = Some (KStoptr p) ⟶
              (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
        by blast
      have typeNew:"Type (Accounts st' (Address e)) = Some (atype.Contract c_fi)"
        using IH1 fiC atype_same[OF 1(8)] st'Final unfolding StateInvariant_def
        using "11.prems"(2) atype_same by presburger
      have ptrNew:"∀t l p.
          (type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KStoptr p) ⟶
          (∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p)"
      proof (intro allI impI)
        fix t l p
        assume in1:"(type.Storage t, Stackloc l) |∈| fmran (Denvalue e) ∧ accessStore l (Stack st') = Some (KStoptr p)"
        have "accessStore l (Stack st) = Some (KStoptr p)" using in1 sameStores by simp
        then show "∃t' l'. (type.Storage t', Storeloc l') |∈| fmran (Denvalue e) ∧ CompStoType t' t l' p"
          using fiPtr in1 by blast
      qed
      show "fullyInitialised e (Accounts st') (Stack st')"
        unfolding fullyInitialised_def using fiC typeNew ptrNew by blast

    next
      show "⋀locs t. accessTypeStore locs (Memory st) = Some t ⟹ accessTypeStore locs (Memory st') = Some t"
        using sameStores by auto
    next
      show "⋀locs v.
       accessStore locs (Memory st) = Some (MPointer v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MPointer v')"
        using sameStores by auto
    next
      show "⋀locs v. accessStore locs (Memory st) = Some (MValue v) ⟹ ∃v'. accessStore locs (Memory st') = Some (MValue v')"
        using sameStores by auto
    next
      show "⋀i loc.
       i < Toploc (Memory st) ∧ LSubPrefL2 loc (ShowLnat i) ⟹
       accessStore loc (Memory st) = None ⟹ accessStore loc (Memory st') = None"
        using sameStores by auto
    next
      show "Toploc (Memory st) ≤ Toploc (Memory st')"
        using sameStores by auto
    qed
  qed
qed

end


end