thys/Hoare_tm2.thy
changeset 17 86918b45b2e6
parent 16 0352ad5ee9c5
child 19 087d82632852
equal deleted inserted replaced
16:0352ad5ee9c5 17:86918b45b2e6
     1 header {* 
     1 header {* 
     2   Separation logic for Turing Machines
     2   Separation logic for Turing Machines
     3 *}
     3 *}
     4 
     4 
     5 theory Hoare_tm
     5 theory Hoare_tm2
     6 imports Hoare_gen 
     6 imports Hoare_gen 
     7         My_block 
     7         My_block 
     8         Data_slot
     8         Data_slot
     9         "~~/src/HOL/Library/FinFun_Syntax"
     9         "~~/src/HOL/Library/FinFun_Syntax"
    10 begin
    10 begin
   122 fun trset_of :: "tconf \<Rightarrow> tresource set"
   122 fun trset_of :: "tconf \<Rightarrow> tresource set"
   123   where "trset_of (faults, prog, pc, pos, m) = 
   123   where "trset_of (faults, prog, pc, pos, m) = 
   124                tprog_set prog \<union> tpc_set pc \<union> tpos_set pos \<union> tmem_set m \<union> tfaults_set faults"
   124                tprog_set prog \<union> tpc_set pc \<union> tpos_set pos \<union> tmem_set m \<union> tfaults_set faults"
   125 
   125 
   126 interpretation tm: sep_exec tstep trset_of .
   126 interpretation tm: sep_exec tstep trset_of .
   127 
       
   128 
   127 
   129 
   128 
   130 section {* Assembly language for TMs and its program logic *}
   129 section {* Assembly language for TMs and its program logic *}
   131 
   130 
   132 subsection {* The assembly language *}
   131 subsection {* The assembly language *}
   409 lemma codeD: "(st i ** sg {TC i inst} ** r) (trset_of (ft, prog, i', pos, mem))
   408 lemma codeD: "(st i ** sg {TC i inst} ** r) (trset_of (ft, prog, i', pos, mem))
   410        \<Longrightarrow> prog $ i = Some inst"
   409        \<Longrightarrow> prog $ i = Some inst"
   411 proof -
   410 proof -
   412   assume "(st i ** sg {TC i inst} ** r) (trset_of (ft, prog, i', pos, mem))"
   411   assume "(st i ** sg {TC i inst} ** r) (trset_of (ft, prog, i', pos, mem))"
   413   thus ?thesis
   412   thus ?thesis
   414     apply(unfold sep_conj_def set_ins_def sg_def trset_of.simps tpn_set_def)
   413     apply(unfold sep_conj_def set_ins_def sg_def trset_of.simps tpn_set_def) 
   415     
       
   416     by auto
   414     by auto
   417 qed
   415 qed
   418 
   416 
   419 lemma memD: "((tm a v) ** r) (trset_of (ft, prog, i, pos, mem))  \<Longrightarrow> mem a = Some v"
   417 lemma memD: "((tm a v) ** r) (trset_of (ft, prog, i, pos, mem))  \<Longrightarrow> mem $ a = Some v"
   420 proof -
   418 proof -
   421   assume "((tm a v) ** r) (trset_of (ft, prog, i, pos, mem))"
   419   assume "((tm a v) ** r) (trset_of (ft, prog, i, pos, mem))"
   422   from stimes_sgD[OF this[unfolded trset_of.simps tpn_set_def tm_def]]
   420   from stimes_sgD[OF this[unfolded trset_of.simps tpn_set_def tm_def]]
   423   have "{TM a v} \<subseteq> {TC i inst |i inst. prog i = Some inst} \<union> {TAt i} \<union> 
   421   have "{TM a v} \<subseteq> {TC i inst |i inst. prog $ i = Some inst} \<union> {TAt i} \<union> 
   424     {TPos pos} \<union> {TM i n |i n. mem i = Some n} \<union> {TFaults ft}" by simp
   422     {TPos pos} \<union> {TM i n |i n. mem $ i = Some n} \<union> {TFaults ft}" by simp
   425   thus ?thesis by auto
   423   thus ?thesis by auto
   426 qed
   424 qed
   427 
   425 
   428 lemma t_hoare_seq: 
   426 lemma t_hoare_seq: 
   429   assumes a1: "\<And> i j. \<lbrace>st i \<and>* p\<rbrace> i:[c1]:j \<lbrace>st j \<and>* q\<rbrace>"
   427   assumes a1: "\<And> i j. \<lbrace>st i \<and>* p\<rbrace> i:[c1]:j \<lbrace>st j \<and>* q\<rbrace>"
  1212                              ({TM a v} \<subseteq> tmem_set mem)"
  1210                              ({TM a v} \<subseteq> tmem_set mem)"
  1213   by (unfold tpn_set_def, auto)
  1211   by (unfold tpn_set_def, auto)
  1214 
  1212 
  1215 lemma tmem_set_upd: 
  1213 lemma tmem_set_upd: 
  1216   "{TM a v} \<subseteq> tmem_set mem \<Longrightarrow> 
  1214   "{TM a v} \<subseteq> tmem_set mem \<Longrightarrow> 
  1217         tmem_set (mem(a:=Some v')) = ((tmem_set mem) - {TM a v}) \<union> {TM a v'}"
  1215         tmem_set (mem(a $:=Some v')) = ((tmem_set mem) - {TM a v}) \<union> {TM a v'}"
  1218   by (unfold tpn_set_def, auto)
  1216 apply(unfold tpn_set_def) 
       
  1217 apply(auto)
       
  1218 apply (metis finfun_upd_apply option.inject)
       
  1219 apply (metis finfun_upd_apply_other)
       
  1220 by (metis finfun_upd_apply_other option.inject)
       
  1221 
  1219 
  1222 
  1220 lemma tmem_set_disj: "{TM a v} \<subseteq> tmem_set mem \<Longrightarrow> 
  1223 lemma tmem_set_disj: "{TM a v} \<subseteq> tmem_set mem \<Longrightarrow> 
  1221                             {TM a v'} \<inter>  (tmem_set mem - {TM a v}) = {}"
  1224                             {TM a v'} \<inter>  (tmem_set mem - {TM a v}) = {}"
  1222   by (unfold tpn_set_def, auto)
  1225   by (unfold tpn_set_def, auto)
  1223 
  1226 
  1224 lemma smem_upd: "((tm a v) ** r) (trset_of (f, x, y, z, mem))  \<Longrightarrow> 
  1227 lemma smem_upd: "((tm a v) ** r) (trset_of (f, x, y, z, mem))  \<Longrightarrow> 
  1225                     ((tm a v') ** r) (trset_of (f, x, y, z, mem(a := Some v')))"
  1228                     ((tm a v') ** r) (trset_of (f, x, y, z, mem(a $:= Some v')))"
  1226 proof -
  1229 proof -
  1227   have eq_s: "(tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tmem_set mem \<union> tfaults_set f - {TM a v}) =
  1230   have eq_s: "(tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tmem_set mem \<union> tfaults_set f - {TM a v}) =
  1228     (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)"
  1231     (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)"
  1229     by (unfold tpn_set_def, auto)
  1232     by (unfold tpn_set_def, auto)
  1230   assume g: "(tm a v \<and>* r) (trset_of (f, x, y, z, mem))"
  1233   assume g: "(tm a v \<and>* r) (trset_of (f, x, y, z, mem))"
  1233   hence h0: "r (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)"
  1236   hence h0: "r (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)"
  1234     by(sep_drule stimes_sgD, clarify, unfold eq_s, auto)
  1237     by(sep_drule stimes_sgD, clarify, unfold eq_s, auto)
  1235   from h TM_in_simp have "{TM a v} \<subseteq> tmem_set mem"
  1238   from h TM_in_simp have "{TM a v} \<subseteq> tmem_set mem"
  1236     by(sep_drule stimes_sgD, auto)
  1239     by(sep_drule stimes_sgD, auto)
  1237   from tmem_set_upd [OF this] tmem_set_disj[OF this]
  1240   from tmem_set_upd [OF this] tmem_set_disj[OF this]
  1238   have h2: "tmem_set (mem(a \<mapsto> v')) = {TM a v'} \<union> (tmem_set mem - {TM a v})" 
  1241   have h2: "tmem_set (mem(a $:= Some v')) = {TM a v'} \<union> (tmem_set mem - {TM a v})" 
  1239            "{TM a v'} \<inter> (tmem_set mem - {TM a v}) = {}" by auto
  1242            "{TM a v'} \<inter> (tmem_set mem - {TM a v}) = {}" by auto
  1240   show ?thesis
  1243   show ?thesis
  1241   proof -
  1244   proof -
  1242     have "(tm a v' ** r) (tmem_set (mem(a \<mapsto> v')) \<union> tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tfaults_set f)"
  1245     have "(tm a v' ** r) (tmem_set (mem(a $:= Some v')) \<union> tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tfaults_set f)"
  1243     proof(rule sep_conjI)
  1246     proof(rule sep_conjI)
  1244       show "tm a v' ({TM a v'})" by (unfold tm_def sg_def, simp)
  1247       show "tm a v' ({TM a v'})" by (unfold tm_def sg_def, simp)
  1245     next
  1248     next
  1246       from h0 show "r (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)" .
  1249       from h0 show "r (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)" .
  1247     next
  1250     next
  1248       show "{TM a v'} ## tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f"
  1251       show "{TM a v'} ## tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f"
  1249       proof -
  1252       proof -
  1250         from g have " mem a = Some v"
  1253         from g have " mem $ a = Some v"
  1251           by(sep_frule memD, simp)
  1254           by(sep_frule memD, simp)
  1252         thus "?thesis"
  1255         thus "?thesis"
  1253           by(unfold tpn_set_def set_ins_def, auto)
  1256           by(unfold tpn_set_def set_ins_def, auto)
  1254       qed
  1257       qed
  1255     next
  1258     next
  1256       show "tmem_set (mem(a \<mapsto> v')) \<union> tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tfaults_set f =
  1259       show "tmem_set (mem(a $:= Some v')) \<union> tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> tfaults_set f =
  1257     {TM a v'} + (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)"
  1260     {TM a v'} + (tprog_set x \<union> tpc_set y \<union> tpos_set z \<union> (tmem_set mem - {TM a v}) \<union> tfaults_set f)"
  1258         by (unfold h2(1) set_ins_def eq_s, auto)
  1261         by (unfold h2(1) set_ins_def eq_s, auto)
  1259     qed
  1262     qed
  1260     thus ?thesis 
  1263     thus ?thesis 
  1261       apply (unfold trset_of.simps)
  1264       apply (unfold trset_of.simps)
  1276           \<lbrace>st (Suc i) \<and>* ps p \<and>* tm p Bk\<rbrace>"
  1279           \<lbrace>st (Suc i) \<and>* ps p \<and>* tm p Bk\<rbrace>"
  1277     proof(fold eq_j, unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1280     proof(fold eq_j, unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1278       fix ft prog cs i' mem r
  1281       fix ft prog cs i' mem r
  1279       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* sg {TC i ((W0, j), W0, j)})
  1282       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* sg {TC i ((W0, j), W0, j)})
  1280               (trset_of (ft, prog, cs, i', mem))"
  1283               (trset_of (ft, prog, cs, i', mem))"
  1281       from h have "prog i = Some ((W0, j), W0, j)"
  1284       from h have "prog $ i = Some ((W0, j), W0, j)"
  1282         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v" in codeD)
  1285         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v" in codeD)
  1283         by(simp add: sep_conj_ac)
  1286         by(simp add: sep_conj_ac)
  1284       from h and this have stp:
  1287       from h and this have stp:
  1285         "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, j, i', mem(i'\<mapsto> Bk))" (is "?x = ?y")
  1288         "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, j, i', mem(i' $:= Some Bk))" (is "?x = ?y")
  1286         apply(sep_frule psD)
  1289         apply(sep_frule psD)
  1287         apply(sep_frule stD)
  1290         apply(sep_frule stD)
  1288         apply(sep_frule memD, simp)
  1291         apply(sep_frule memD, simp)
  1289         by(cases v, unfold tm.run_def, auto)
  1292         by(cases v, unfold tm.run_def, auto)
  1290       from h have "i' = p"
  1293       from h have "i' = p"
  1326           \<lbrace>ps p \<and>* st ?j \<and>* tm p Oc\<rbrace>"
  1329           \<lbrace>ps p \<and>* st ?j \<and>* tm p Oc\<rbrace>"
  1327     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1330     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1328       fix ft prog cs i' mem r
  1331       fix ft prog cs i' mem r
  1329       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* sg {TC i ((W1, ?j), W1, ?j)})
  1332       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* sg {TC i ((W1, ?j), W1, ?j)})
  1330               (trset_of (ft, prog, cs, i', mem))"
  1333               (trset_of (ft, prog, cs, i', mem))"
  1331       from h have "prog i = Some ((W1, ?j), W1, ?j)"
  1334       from h have "prog $ i = Some ((W1, ?j), W1, ?j)"
  1332         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v" in codeD)
  1335         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v" in codeD)
  1333         by(simp add: sep_conj_ac)
  1336         by(simp add: sep_conj_ac)
  1334       from h and this have stp:
  1337       from h and this have stp:
  1335         "tm.run 1 (ft, prog, cs, i', mem) = 
  1338         "tm.run 1 (ft, prog, cs, i', mem) = 
  1336                      (ft, prog, ?j, i', mem(i'\<mapsto> Oc))" (is "?x = ?y")
  1339                      (ft, prog, ?j, i', mem(i' $:= Some Oc))" (is "?x = ?y")
  1337         apply(sep_frule psD)
  1340         apply(sep_frule psD)
  1338         apply(sep_frule stD)
  1341         apply(sep_frule stD)
  1339         apply(sep_frule memD, simp)
  1342         apply(sep_frule memD, simp)
  1340         by(cases v, unfold tm.run_def, auto)
  1343         by(cases v, unfold tm.run_def, auto)
  1341       from h have "i' = p"
  1344       from h have "i' = p"
  1378           \<lbrace>st ?j \<and>* tm p v2 \<and>* ps (p - 1)\<rbrace>"
  1381           \<lbrace>st ?j \<and>* tm p v2 \<and>* ps (p - 1)\<rbrace>"
  1379     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1382     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1380       fix ft prog cs i' mem r
  1383       fix ft prog cs i' mem r
  1381       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v2 \<and>* sg {TC i ((L, ?j), L, ?j)}) 
  1384       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v2 \<and>* sg {TC i ((L, ?j), L, ?j)}) 
  1382                        (trset_of (ft, prog, cs, i',  mem))"
  1385                        (trset_of (ft, prog, cs, i',  mem))"
  1383       from h have "prog i = Some ((L, ?j), L, ?j)"
  1386       from h have "prog $ i = Some ((L, ?j), L, ?j)"
  1384         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v2" in codeD)
  1387         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v2" in codeD)
  1385         by(simp add: sep_conj_ac)
  1388         by(simp add: sep_conj_ac)
  1386       from h and this have stp:
  1389       from h and this have stp:
  1387         "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, ?j, i' - 1, mem)" (is "?x = ?y")
  1390         "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, ?j, i' - 1, mem)" (is "?x = ?y")
  1388         apply(sep_frule psD)
  1391         apply(sep_frule psD)
  1436           \<lbrace>st ?j \<and>* tm p v1 \<and>* ps (p + 1)\<rbrace>"
  1439           \<lbrace>st ?j \<and>* tm p v1 \<and>* ps (p + 1)\<rbrace>"
  1437     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1440     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1438       fix ft prog cs i' mem r
  1441       fix ft prog cs i' mem r
  1439       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v1 \<and>* sg {TC i ((R, ?j), R, ?j)}) 
  1442       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v1 \<and>* sg {TC i ((R, ?j), R, ?j)}) 
  1440                        (trset_of (ft, prog, cs, i',  mem))"
  1443                        (trset_of (ft, prog, cs, i',  mem))"
  1441       from h have "prog i = Some ((R, ?j), R, ?j)"
  1444       from h have "prog $ i = Some ((R, ?j), R, ?j)"
  1442         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v1" in codeD)
  1445         apply(rule_tac r = "r \<and>* ps p \<and>* tm p v1" in codeD)
  1443         by(simp add: sep_conj_ac)
  1446         by(simp add: sep_conj_ac)
  1444       from h and this have stp:
  1447       from h and this have stp:
  1445         "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, ?j, i'+ 1, mem)" (is "?x = ?y")
  1448         "tm.run 1 (ft, prog, cs, i', mem) = (ft, prog, ?j, i'+ 1, mem)" (is "?x = ?y")
  1446         apply(sep_frule psD)
  1449         apply(sep_frule psD)
  1492     show "\<lbrace>one p \<and>* ps p \<and>* st i\<rbrace>  sg {TC i ((W0, ?j), W1, e)} \<lbrace>one p \<and>* ps p \<and>* st e\<rbrace>"
  1495     show "\<lbrace>one p \<and>* ps p \<and>* st i\<rbrace>  sg {TC i ((W0, ?j), W1, e)} \<lbrace>one p \<and>* ps p \<and>* st e\<rbrace>"
  1493     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1496     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1494       fix ft prog cs pc mem r
  1497       fix ft prog cs pc mem r
  1495       assume h: "(r \<and>* one p \<and>* ps p \<and>* st i \<and>* sg {TC i ((W0, ?j), W1, e)}) 
  1498       assume h: "(r \<and>* one p \<and>* ps p \<and>* st i \<and>* sg {TC i ((W0, ?j), W1, e)}) 
  1496         (trset_of (ft, prog, cs, pc, mem))"
  1499         (trset_of (ft, prog, cs, pc, mem))"
  1497       from h have k1: "prog i = Some ((W0, ?j), W1, e)"
  1500       from h have k1: "prog $ i = Some ((W0, ?j), W1, e)"
  1498         apply(rule_tac r = "r \<and>* one p \<and>* ps p" in codeD)
  1501         apply(rule_tac r = "r \<and>* one p \<and>* ps p" in codeD)
  1499         by(simp add: sep_conj_ac)
  1502         by(simp add: sep_conj_ac)
  1500       from h have k2: "pc = p"
  1503       from h have k2: "pc = p"
  1501         by(sep_drule psD, simp)
  1504         by(sep_drule psD, simp)
  1502       from h and this have k3: "mem pc = Some Oc"
  1505       from h and this have k3: "mem $ pc = Some Oc"
  1503         apply(unfold one_def)
  1506         apply(unfold one_def)
  1504         by(sep_drule memD, simp)
  1507         by(sep_drule memD, simp)
  1505       from h k1 k2 k3 have stp:
  1508       from h k1 k2 k3 have stp:
  1506         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, e, pc, mem)" (is "?x = ?y")
  1509         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, e, pc, mem)" (is "?x = ?y")
  1507         apply(sep_drule stD)
  1510         apply(sep_drule stD)
  1508         by(unfold tm.run_def, auto)
  1511         apply(unfold tm.run_def)
       
  1512         apply(auto)
       
  1513         by (metis finfun_upd_triv)
       
  1514 
  1509       from h k2 
  1515       from h k2 
  1510       have "(r \<and>* one p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, ?j), W1, e)})  (trset_of ?x)"
  1516       have "(r \<and>* one p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, ?j), W1, e)})  (trset_of ?x)"
  1511         apply(unfold stp)
  1517         apply(unfold stp)
  1512         by(sep_drule st_upd, simp add: sep_conj_ac)
  1518         by(sep_drule st_upd, simp add: sep_conj_ac)
  1513       thus "\<exists>k.(r \<and>* one p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, ?j), W1, e)})
  1519       thus "\<exists>k.(r \<and>* one p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, ?j), W1, e)})
  1576     show "\<lbrace>ps p \<and>* st i \<and>* zero p\<rbrace>  sg {TC i ((W0, ?j), W1, e)} \<lbrace>ps p \<and>*  zero p \<and>* st ?j \<rbrace>"
  1582     show "\<lbrace>ps p \<and>* st i \<and>* zero p\<rbrace>  sg {TC i ((W0, ?j), W1, e)} \<lbrace>ps p \<and>*  zero p \<and>* st ?j \<rbrace>"
  1577     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1583     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1578       fix ft prog cs pc mem r
  1584       fix ft prog cs pc mem r
  1579       assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* sg {TC i ((W0, ?j), W1, e)})
  1585       assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* sg {TC i ((W0, ?j), W1, e)})
  1580         (trset_of (ft, prog, cs, pc, mem))"
  1586         (trset_of (ft, prog, cs, pc, mem))"
  1581       from h have k1: "prog i = Some ((W0, ?j), W1, e)"
  1587       from h have k1: "prog $ i = Some ((W0, ?j), W1, e)"
  1582         apply(rule_tac r = "r \<and>* zero p \<and>* ps p" in codeD)
  1588         apply(rule_tac r = "r \<and>* zero p \<and>* ps p" in codeD)
  1583         by(simp add: sep_conj_ac)
  1589         by(simp add: sep_conj_ac)
  1584       from h have k2: "pc = p"
  1590       from h have k2: "pc = p"
  1585         by(sep_drule psD, simp)
  1591         by(sep_drule psD, simp)
  1586       from h and this have k3: "mem pc = Some Bk"
  1592       from h and this have k3: "mem $ pc = Some Bk"
  1587         apply(unfold zero_def)
  1593         apply(unfold zero_def)
  1588         by(sep_drule memD, simp)
  1594         by(sep_drule memD, simp)
  1589       from h k1 k2 k3 have stp:
  1595       from h k1 k2 k3 have stp:
  1590         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, ?j, pc, mem)" (is "?x = ?y")
  1596         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, ?j, pc, mem)" (is "?x = ?y")
  1591         apply(sep_drule stD)
  1597         apply(sep_drule stD)
  1592         by(unfold tm.run_def, auto)
  1598         apply(unfold tm.run_def)
       
  1599         apply(auto)
       
  1600         by (metis finfun_upd_triv)
  1593       from h k2 
  1601       from h k2 
  1594       have "(r \<and>* zero p \<and>* ps p \<and>* st ?j \<and>* sg {TC i ((W0, ?j), W1, e)})  (trset_of ?x)"
  1602       have "(r \<and>* zero p \<and>* ps p \<and>* st ?j \<and>* sg {TC i ((W0, ?j), W1, e)})  (trset_of ?x)"
  1595         apply (unfold stp)
  1603         apply (unfold stp)
  1596         by (sep_drule st_upd[where i''="?j"], auto simp:sep_conj_ac)
  1604         by (sep_drule st_upd[where i''="?j"], auto simp:sep_conj_ac)
  1597       thus "\<exists>k. (r \<and>* ps p \<and>* zero p \<and>* st ?j \<and>*  sg {TC i ((W0, ?j), W1, e)})
  1605       thus "\<exists>k. (r \<and>* ps p \<and>* zero p \<and>* st ?j \<and>*  sg {TC i ((W0, ?j), W1, e)})
  1623     show "\<lbrace>ps p \<and>* st i \<and>* zero p\<rbrace>  sg {TC i ((W0, e), W1, ?j)} \<lbrace>ps p \<and>* st e \<and>* zero p\<rbrace>"
  1631     show "\<lbrace>ps p \<and>* st i \<and>* zero p\<rbrace>  sg {TC i ((W0, e), W1, ?j)} \<lbrace>ps p \<and>* st e \<and>* zero p\<rbrace>"
  1624     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1632     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1625       fix ft prog cs pc mem r
  1633       fix ft prog cs pc mem r
  1626       assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* sg {TC i ((W0, e), W1, ?j)})
  1634       assume h: "(r \<and>* ps p \<and>* st i \<and>* zero p \<and>* sg {TC i ((W0, e), W1, ?j)})
  1627         (trset_of (ft, prog, cs, pc, mem))"
  1635         (trset_of (ft, prog, cs, pc, mem))"
  1628       from h have k1: "prog i = Some ((W0, e), W1, ?j)"
  1636       from h have k1: "prog $ i = Some ((W0, e), W1, ?j)"
  1629         apply(rule_tac r = "r \<and>* zero p \<and>* ps p" in codeD)
  1637         apply(rule_tac r = "r \<and>* zero p \<and>* ps p" in codeD)
  1630         by(simp add: sep_conj_ac)
  1638         by(simp add: sep_conj_ac)
  1631       from h have k2: "pc = p"
  1639       from h have k2: "pc = p"
  1632         by(sep_drule psD, simp)
  1640         by(sep_drule psD, simp)
  1633       from h and this have k3: "mem pc = Some Bk"
  1641       from h and this have k3: "mem $ pc = Some Bk"
  1634         apply(unfold zero_def)
  1642         apply(unfold zero_def)
  1635         by(sep_drule memD, simp)
  1643         by(sep_drule memD, simp)
  1636       from h k1 k2 k3 have stp:
  1644       from h k1 k2 k3 have stp:
  1637         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, e, pc, mem)" (is "?x = ?y")
  1645         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, e, pc, mem)" (is "?x = ?y")
  1638         apply(sep_drule stD)
  1646         apply(sep_drule stD)
  1639         by(unfold tm.run_def, auto)
  1647         apply(unfold tm.run_def)
       
  1648         apply(auto)
       
  1649         by (metis finfun_upd_triv)  
  1640       from h k2 
  1650       from h k2 
  1641       have "(r \<and>* zero p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, e), W1, ?j)})  (trset_of ?x)"
  1651       have "(r \<and>* zero p \<and>* ps p \<and>* st e \<and>* sg {TC i ((W0, e), W1, ?j)})  (trset_of ?x)"
  1642         apply(unfold stp)
  1652         apply(unfold stp)
  1643         by(sep_drule st_upd, simp add: sep_conj_ac)
  1653         by(sep_drule st_upd, simp add: sep_conj_ac)
  1644       thus "\<exists>k. (r \<and>* ps p \<and>* st e \<and>* zero p \<and>* sg {TC i ((W0, e), W1, ?j)})
  1654       thus "\<exists>k. (r \<and>* ps p \<and>* st e \<and>* zero p \<and>* sg {TC i ((W0, e), W1, ?j)})
  1700           \<lbrace>ps p \<and>* st ?j \<and>* tm p Oc\<rbrace>"
  1710           \<lbrace>ps p \<and>* st ?j \<and>* tm p Oc\<rbrace>"
  1701     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1711     proof(unfold tassemble_to.simps tm.Hoare_gen_def sep_conj_ac, clarify)
  1702       fix ft prog cs pc mem r
  1712       fix ft prog cs pc mem r
  1703       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p Oc \<and>* sg {TC i ((W0, e), W1, ?j)})
  1713       assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p Oc \<and>* sg {TC i ((W0, e), W1, ?j)})
  1704         (trset_of (ft, prog, cs, pc, mem))"
  1714         (trset_of (ft, prog, cs, pc, mem))"
  1705       from h have k1: "prog i = Some ((W0, e), W1, ?j)"
  1715       from h have k1: "prog $ i = Some ((W0, e), W1, ?j)"
  1706         apply(rule_tac r = "r \<and>* tm p Oc \<and>* ps p" in codeD)
  1716         apply(rule_tac r = "r \<and>* tm p Oc \<and>* ps p" in codeD)
  1707         by(simp add: sep_conj_ac)
  1717         by(simp add: sep_conj_ac)
  1708       from h have k2: "pc = p"
  1718       from h have k2: "pc = p"
  1709         by(sep_drule psD, simp)
  1719         by(sep_drule psD, simp)
  1710       from h and this have k3: "mem pc = Some Oc"
  1720       from h and this have k3: "mem $ pc = Some Oc"
  1711         by(sep_drule memD, simp)
  1721         by(sep_drule memD, simp)
  1712       from h k1 k2 k3 have stp:
  1722       from h k1 k2 k3 have stp:
  1713         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, ?j, pc, mem)" (is "?x = ?y")
  1723         "tm.run 1 (ft, prog, cs, pc, mem) = (ft, prog, ?j, pc, mem)" (is "?x = ?y")
  1714         apply(sep_drule stD)
  1724         apply(sep_drule stD)
  1715         by(unfold tm.run_def, auto)
  1725         apply(unfold tm.run_def)
       
  1726         apply(auto)
       
  1727         by (metis finfun_upd_triv)
  1716       from h k2 
  1728       from h k2 
  1717       have "(r \<and>* tm p Oc \<and>* ps p \<and>* st ?j \<and>* sg {TC i ((W0, e), W1, ?j)})  (trset_of ?x)"
  1729       have "(r \<and>* tm p Oc \<and>* ps p \<and>* st ?j \<and>* sg {TC i ((W0, e), W1, ?j)})  (trset_of ?x)"
  1718         apply(unfold stp)
  1730         apply(unfold stp)
  1719         by(sep_drule st_upd, simp add: sep_conj_ac)
  1731         by(sep_drule st_upd, simp add: sep_conj_ac)
  1720       thus "\<exists>k. (r \<and>* ps p \<and>* st ?j \<and>* tm p Oc \<and>* sg {TC i ((W0, e), W1, ?j)})
  1732       thus "\<exists>k. (r \<and>* ps p \<and>* st ?j \<and>* tm p Oc \<and>* sg {TC i ((W0, e), W1, ?j)})
  1739   "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace>  i:[jmp e]:j \<lbrace>st e \<and>* ps p \<and>* tm p v\<rbrace>"
  1751   "\<lbrace>st i \<and>* ps p \<and>* tm p v\<rbrace>  i:[jmp e]:j \<lbrace>st e \<and>* ps p \<and>* tm p v\<rbrace>"
  1740 proof(unfold jmp_def tm.Hoare_gen_def tassemble_to.simps sep_conj_ac, clarify)
  1752 proof(unfold jmp_def tm.Hoare_gen_def tassemble_to.simps sep_conj_ac, clarify)
  1741   fix ft prog cs pos mem r
  1753   fix ft prog cs pos mem r
  1742   assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* <(j = i + 1)> \<and>* sg {TC i ((W0, e), W1, e)})
  1754   assume h: "(r \<and>* ps p \<and>* st i \<and>* tm p v \<and>* <(j = i + 1)> \<and>* sg {TC i ((W0, e), W1, e)})
  1743     (trset_of (ft, prog, cs, pos, mem))"
  1755     (trset_of (ft, prog, cs, pos, mem))"
  1744   from h have k1: "prog i = Some ((W0, e), W1, e)"
  1756   from h have k1: "prog $ i = Some ((W0, e), W1, e)"
  1745     apply(rule_tac r = "r \<and>* <(j = i + 1)> \<and>* tm p v \<and>* ps p" in codeD)
  1757     apply(rule_tac r = "r \<and>* <(j = i + 1)> \<and>* tm p v \<and>* ps p" in codeD)
  1746     by(simp add: sep_conj_ac)
  1758     by(simp add: sep_conj_ac)
  1747   from h have k2: "p = pos"
  1759   from h have k2: "p = pos"
  1748     by(sep_drule psD, simp)
  1760     by(sep_drule psD, simp)
  1749   from h k2 have k3: "mem pos = Some v"
  1761   from h k2 have k3: "mem $ pos = Some v"
  1750     by(sep_drule memD, simp)
  1762     by(sep_drule memD, simp)
  1751   from h k1 k2 k3 have 
  1763   from h k1 k2 k3 have 
  1752     stp: "tm.run 1 (ft, prog, cs, pos, mem) = (ft, prog, e, pos, mem)" (is "?x = ?y")
  1764     stp: "tm.run 1 (ft, prog, cs, pos, mem) = (ft, prog, e, pos, mem)" (is "?x = ?y")
  1753     apply(sep_drule stD)
  1765     apply(sep_drule stD)
  1754     by(unfold tm.run_def, cases "mem pos", simp, cases v, auto)
  1766     apply(unfold tm.run_def)
       
  1767     apply(cases "mem $ pos")
       
  1768     apply(simp)
       
  1769     apply(cases v)
       
  1770     apply(auto)
       
  1771     apply (metis finfun_upd_triv)
       
  1772     by (metis finfun_upd_triv)
  1755   from h k2 
  1773   from h k2 
  1756   have "(r \<and>* ps p \<and>* st e \<and>* tm p v \<and>* <(j = i + 1)> \<and>* 
  1774   have "(r \<and>* ps p \<and>* st e \<and>* tm p v \<and>* <(j = i + 1)> \<and>* 
  1757            sg {TC i ((W0, e), W1, e)}) (trset_of ?x)"
  1775            sg {TC i ((W0, e), W1, e)}) (trset_of ?x)"
  1758     apply(unfold stp)
  1776     apply(unfold stp)
  1759     by(sep_drule st_upd, simp add: sep_conj_ac)
  1777     by(sep_drule st_upd, simp add: sep_conj_ac)
  4211 
  4229 
  4212 lemma sep_conj_cond3 : "(<cond1> \<and>* <cond2>) = <(cond1 \<and> cond2)>"
  4230 lemma sep_conj_cond3 : "(<cond1> \<and>* <cond2>) = <(cond1 \<and> cond2)>"
  4213   by (smt cond_eqI cond_true_eq sep_conj_commute sep_conj_empty)
  4231   by (smt cond_eqI cond_true_eq sep_conj_commute sep_conj_empty)
  4214 
  4232 
  4215 lemma sep_conj_cond4 : "(<cond1> \<and>* <cond2> \<and>* r) = (<(cond1 \<and> cond2)> \<and>* r)"
  4233 lemma sep_conj_cond4 : "(<cond1> \<and>* <cond2> \<and>* r) = (<(cond1 \<and> cond2)> \<and>* r)"
  4216   by (metis Hoare_gen.sep_conj_cond3 Hoare_tm.sep_conj_cond3)
  4234 by (metis Hoare_tm2.sep_conj_cond3 sep_conj_assoc)
  4217 
  4235 
  4218 lemmas sep_conj_cond = sep_conj_cond3 sep_conj_cond4 sep_conj_cond 
  4236 lemmas sep_conj_cond = sep_conj_cond3 sep_conj_cond4 sep_conj_cond 
  4219 
  4237 
  4220 lemma hoare_left_until_zero_reps: 
  4238 lemma hoare_left_until_zero_reps: 
  4221   "\<lbrace>st i ** ps v ** zero u ** reps (u + 1) v [k]\<rbrace> 
  4239   "\<lbrace>st i ** ps v ** zero u ** reps (u + 1) v [k]\<rbrace>