thys/uncomputable.thy
changeset 105 2cae8a39803e
parent 103 294576baaeed
child 109 4635641e77cb
equal deleted inserted replaced
104:01f688735b9b 105:2cae8a39803e
    61 | "inv_begin1 n (l, r) = ((l, r) = ([], Oc \<up> n))"
    61 | "inv_begin1 n (l, r) = ((l, r) = ([], Oc \<up> n))"
    62 | "inv_begin2 n (l, r) = (\<exists> i j. i > 0 \<and> i + j = n \<and> (l, r) = (Oc \<up> i, Oc \<up> j))"
    62 | "inv_begin2 n (l, r) = (\<exists> i j. i > 0 \<and> i + j = n \<and> (l, r) = (Oc \<up> i, Oc \<up> j))"
    63 | "inv_begin3 n (l, r) = (n > 0 \<and> (l, tl r) = (Bk # Oc \<up> n, []))"
    63 | "inv_begin3 n (l, r) = (n > 0 \<and> (l, tl r) = (Bk # Oc \<up> n, []))"
    64 | "inv_begin4 n (l, r) = (n > 0 \<and> (l, r) = (Oc \<up> n, [Bk, Oc]) \<or> (l, r) = (Oc \<up> (n - 1), [Oc, Bk, Oc]))"
    64 | "inv_begin4 n (l, r) = (n > 0 \<and> (l, r) = (Oc \<up> n, [Bk, Oc]) \<or> (l, r) = (Oc \<up> (n - 1), [Oc, Bk, Oc]))"
    65 
    65 
    66 
       
    67 
       
    68 fun inv_begin :: "nat \<Rightarrow> config \<Rightarrow> bool"
    66 fun inv_begin :: "nat \<Rightarrow> config \<Rightarrow> bool"
    69   where
    67   where
    70   "inv_begin n (s, tp) = 
    68   "inv_begin n (s, tp) = 
    71         (if s = 0 then inv_begin0 n tp else
    69         (if s = 0 then inv_begin0 n tp else
    72          if s = 1 then inv_begin1 n tp else
    70          if s = 1 then inv_begin1 n tp else
    78 lemma [elim]: "\<lbrakk>0 < i; 0 < j\<rbrakk> \<Longrightarrow> 
    76 lemma [elim]: "\<lbrakk>0 < i; 0 < j\<rbrakk> \<Longrightarrow> 
    79   \<exists>ia>0. ia + j - Suc 0 = i + j \<and> Oc # Oc \<up> i = Oc \<up> ia"
    77   \<exists>ia>0. ia + j - Suc 0 = i + j \<and> Oc # Oc \<up> i = Oc \<up> ia"
    80 by (rule_tac x = "Suc i" in exI, simp)
    78 by (rule_tac x = "Suc i" in exI, simp)
    81 
    79 
    82 lemma inv_begin_step: 
    80 lemma inv_begin_step: 
    83   assumes "inv_begin x cf"
    81   assumes "inv_begin n cf"
    84   and "x > 0"
    82   and "n > 0"
    85   shows "inv_begin x (step0 cf tcopy_begin)"
    83   shows "inv_begin n (step0 cf tcopy_begin)"
    86 using assms
    84 using assms
    87 unfolding tcopy_begin_def
    85 unfolding tcopy_begin_def
    88 apply(case_tac cf)
    86 apply(case_tac cf)
    89 apply(auto simp: numeral split: if_splits)
    87 apply(auto simp: numeral split: if_splits)
    90 apply(case_tac "hd c")
    88 apply(case_tac "hd c")
    92 apply(case_tac c)
    90 apply(case_tac c)
    93 apply(simp_all)
    91 apply(simp_all)
    94 done
    92 done
    95 
    93 
    96 lemma inv_begin_steps: 
    94 lemma inv_begin_steps: 
    97   assumes "inv_begin x cf"
    95   assumes "inv_begin n cf"
    98   and "x > 0"
    96   and "n > 0"
    99   shows "inv_begin x (steps0 cf tcopy_begin stp)"
    97   shows "inv_begin n (steps0 cf tcopy_begin stp)"
   100 apply(induct stp)
    98 apply(induct stp)
   101 apply(simp add: assms)
    99 apply(simp add: assms)
   102 apply(auto simp del: steps.simps)
   100 apply(auto simp del: steps.simps)
   103 apply(rule_tac inv_begin_step)
   101 apply(rule_tac inv_begin_step)
   104 apply(simp_all add: assms)
   102 apply(simp_all add: assms)
   105 done
   103 done
   106 
   104 
   107 (*
       
   108 lemma begin_partial_correctness:
   105 lemma begin_partial_correctness:
   109   assumes "\<exists>stp. is_final (steps0 (1, [], Oc \<up> n) tcopy_begin stp)"
   106   assumes "is_final (steps0 (1, [], Oc \<up> n) tcopy_begin stp)"
   110   shows "0 < n \<Longrightarrow> {inv_begin1 n} tcopy_begin {inv_begin0 n}"
   107   shows "0 < n \<Longrightarrow> {inv_begin1 n} tcopy_begin {inv_begin0 n}"
   111 using assms 
   108 proof(rule_tac Hoare_haltI)
   112 apply(auto simp add: Hoare_halt_def)
   109   fix l r
   113 apply(rule_tac x="stp" in exI)
   110   assume h: "0 < n" "inv_begin1 n (l, r)"
   114 apply(simp)
   111   have "inv_begin n (steps0 (1, [], Oc \<up> n) tcopy_begin stp)"
   115 apply(case_tac "steps0 (Suc 0, [], Oc \<up> n) tcopy_begin stp")
   112     using h by (rule_tac inv_begin_steps) (simp_all add: inv_begin.simps)
   116 apply(simp)
   113   then show
   117 apply(case_tac n)
   114     "\<exists>stp. is_final (steps0 (1, l, r) tcopy_begin stp) \<and> 
   118 apply(simp)
   115     inv_begin0 n holds_for steps (1, l, r) (tcopy_begin, 0) stp"
   119 apply(simp)
   116     using h assms
   120 apply(case_tac nat)
   117     apply(rule_tac x = stp in exI)
   121 apply(simp)
   118     apply(case_tac "(steps0 (1, [], Oc \<up> n) tcopy_begin stp)", simp add: inv_begin.simps)
   122 apply(simp)
   119     done
   123 *)
   120 qed
   124 
   121 
   125 fun measure_begin_state :: "config \<Rightarrow> nat"
   122 fun measure_begin_state :: "config \<Rightarrow> nat"
   126   where
   123   where
   127   "measure_begin_state (s, l, r) = (if s = 0 then 0 else 5 - s)"
   124   "measure_begin_state (s, l, r) = (if s = 0 then 0 else 5 - s)"
   128 
   125 
   133          if s = 3 then (if r = [] \<or> r = [Bk] then 1 else 0) else
   130          if s = 3 then (if r = [] \<or> r = [Bk] then 1 else 0) else
   134          if s = 4 then length l 
   131          if s = 4 then length l 
   135          else 0)"
   132          else 0)"
   136 
   133 
   137 definition
   134 definition
   138   "begin_LE = measures [measure_begin_state, measure_begin_step]"
   135   "measure_begin = measures [measure_begin_state, measure_begin_step]"
   139 
   136 
   140 lemma [simp]: "\<lbrakk>tl r = []; r \<noteq> []; r \<noteq> [Bk]\<rbrakk> \<Longrightarrow> r = [Oc]"
   137 lemma wf_measure_begin:
   141 by (case_tac r, auto, case_tac a, auto)
   138   shows "wf measure_begin" 
   142 
   139 unfolding measure_begin_def 
   143 
   140 by auto
   144 lemma halt_lemma: 
   141 
   145   "\<lbrakk>wf LE; \<forall>n. (\<not> P (f n) \<longrightarrow> (f (Suc n), (f n)) \<in> LE)\<rbrakk> \<Longrightarrow> \<exists>n. P (f n)"
   142 lemma measure_begin_induct [case_names Step]: 
       
   143   "\<lbrakk>\<And>n. \<not> P (f n) \<Longrightarrow> (f (Suc n), (f n)) \<in> measure_begin\<rbrakk> \<Longrightarrow> \<exists>n. P (f n)"
       
   144 using wf_measure_begin
   146 by (metis wf_iff_no_infinite_down_chain)
   145 by (metis wf_iff_no_infinite_down_chain)
   147 
   146 
   148 lemma begin_halt: 
   147 lemma begin_halts: 
   149   "x > 0 \<Longrightarrow> \<exists> stp. is_final (steps0 (1, [], Oc \<up> x) tcopy_begin stp)"
   148   assumes h: "x > 0"
   150 proof(rule_tac LE = begin_LE in halt_lemma) 
   149   shows "\<exists> stp. is_final (steps0 (1, [], Oc \<up> x) tcopy_begin stp)"
   151   show "wf begin_LE" unfolding begin_LE_def by (auto) 
   150 proof (induct rule: measure_begin_induct) 
   152 next
   151   case (Step n)
   153   assume h: "0 < x"
   152   have "\<not> is_final (steps0 (1, [], Oc \<up> x) tcopy_begin n)" by fact
   154   show "\<forall>n. \<not> is_final (steps (1, [], Oc \<up> x) (tcopy_begin, 0) n) \<longrightarrow>
   153   moreover
   155         (steps (1, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), 
   154   have "inv_begin x (steps0 (1, [], Oc \<up> x) tcopy_begin n)"
   156             steps (1, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> begin_LE"
   155     by (rule_tac inv_begin_steps) (simp_all add: inv_begin.simps h)
   157   proof(rule_tac allI, rule_tac impI)
   156   moreover
   158     fix n
   157   obtain s l r where eq: "(steps0 (1, [], Oc \<up> x) tcopy_begin n) = (s, l, r)"
   159     assume a: "\<not> is_final (steps (1, [], Oc \<up> x) (tcopy_begin, 0) n)"
   158     by (metis measure_begin_state.cases)
   160     have b: "inv_begin x (steps (1, [], Oc \<up> x) (tcopy_begin, 0) n)"
   159   ultimately 
   161       apply(rule_tac inv_begin_steps)
   160   have "(step0 (s, l, r) tcopy_begin, s, l, r) \<in> measure_begin"
   162       apply(simp_all add: inv_begin.simps h)
   161     apply(auto simp: measure_begin_def tcopy_begin_def numeral split: if_splits)
   163       done 
   162     apply(subgoal_tac "r = [Oc]")
   164     obtain s l r where c: "(steps (1, [], Oc \<up> x) (tcopy_begin, 0) n) = (s, l, r)"
   163     apply(auto)
   165       apply(case_tac "steps (1, [], Oc \<up> x) (tcopy_begin, 0) n", auto)
   164     by (metis cell.exhaust list.exhaust tl.simps(2))
   166       done
   165   then 
   167     moreover hence "inv_begin x (s, l, r)" 
   166   show "(steps0 (1, [], Oc \<up> x) tcopy_begin (Suc n), steps0 (1, [], Oc \<up> x) tcopy_begin n) \<in> measure_begin"
   168       using c b by simp
   167     using eq by (simp only: step_red)
   169     ultimately show "(steps (1, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), 
       
   170       steps (1, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> begin_LE"
       
   171       using a 
       
   172     proof(simp del: inv_begin.simps step.simps steps.simps)
       
   173       assume "inv_begin x (s, l, r)" "0 < s"
       
   174       thus "(step (s, l, r) (tcopy_begin, 0), s, l, r) \<in> begin_LE"
       
   175         apply(auto simp: begin_LE_def step.simps tcopy_begin_def numeral split: if_splits)
       
   176         apply(case_tac r, auto, case_tac a, auto)
       
   177         done
       
   178     qed
       
   179   qed
       
   180 qed
   168 qed
   181     
   169 
   182 lemma begin_correct: 
   170 lemma begin_correct: 
   183   "0 < n \<Longrightarrow> {inv_begin1 n} tcopy_begin {inv_begin0 n}"
   171   shows "0 < n \<Longrightarrow> {inv_begin1 n} tcopy_begin {inv_begin0 n}"
   184 proof(rule_tac Hoare_haltI)
   172 using begin_partial_correctness begin_halts by blast
   185   fix l r
       
   186   assume h: "0 < n" "inv_begin1 n (l, r)"
       
   187   then have "\<exists> stp. is_final (steps0 (1, [], Oc \<up> n) tcopy_begin stp)"
       
   188     by (rule_tac begin_halt)    
       
   189   then obtain stp where "is_final (steps0 (1, [], Oc \<up> n) tcopy_begin stp)" ..
       
   190   moreover have "inv_begin n (steps0 (1, [], Oc \<up> n) tcopy_begin stp)"
       
   191     apply(rule_tac inv_begin_steps)
       
   192     using h by(simp_all add: inv_begin.simps)
       
   193   ultimately show
       
   194     "\<exists>stp. is_final (steps0 (1, l, r) tcopy_begin stp) \<and> 
       
   195     inv_begin0 n holds_for steps (1, l, r) (tcopy_begin, 0) stp"
       
   196     using h
       
   197     apply(rule_tac x = stp in exI)
       
   198     apply(case_tac "(steps0 (1, [], Oc \<up> n) tcopy_begin stp)", simp add: inv_begin.simps)
       
   199     done
       
   200 qed
       
   201 
   173 
   202 declare tm_comp.simps [simp del] 
   174 declare tm_comp.simps [simp del] 
   203 declare adjust.simps[simp del] 
   175 declare adjust.simps[simp del] 
   204 declare shift.simps[simp del]
   176 declare shift.simps[simp del]
   205 declare tm_wf.simps[simp del]
   177 declare tm_wf.simps[simp del]
   206 declare step.simps[simp del]
   178 declare step.simps[simp del]
   207 declare steps.simps[simp del]
   179 declare steps.simps[simp del]
   208 
       
   209 
   180 
   210 (* tcopy_loop *)
   181 (* tcopy_loop *)
   211 
   182 
   212 fun 
   183 fun 
   213   inv_loop1_loop :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   184   inv_loop1_loop :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   215   inv_loop5_loop :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   186   inv_loop5_loop :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   216   inv_loop5_exit :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   187   inv_loop5_exit :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   217   inv_loop6_loop :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   188   inv_loop6_loop :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   218   inv_loop6_exit :: "nat \<Rightarrow> tape \<Rightarrow> bool"
   189   inv_loop6_exit :: "nat \<Rightarrow> tape \<Rightarrow> bool"
   219 where
   190 where
   220   "inv_loop1_loop x (l, r) = (\<exists> i j. i + j + 1 = x \<and> (l, r) = (Oc\<up>i, Oc#Oc#Bk\<up>j @ Oc\<up>j) \<and> j > 0)"
   191   "inv_loop1_loop n (l, r) = (\<exists> i j. i + j + 1 = n \<and> (l, r) = (Oc\<up>i, Oc#Oc#Bk\<up>j @ Oc\<up>j) \<and> j > 0)"
   221 | "inv_loop1_exit x (l, r) = ((l, r) = ([], Bk#Oc#Bk\<up>x @ Oc\<up>x) \<and> x > 0)"
   192 | "inv_loop1_exit n (l, r) = (0 < n \<and> (l, r) = ([], Bk#Oc#Bk\<up>n @ Oc\<up>n))"
   222 | "inv_loop5_loop x (l, r) = 
   193 | "inv_loop5_loop x (l, r) = 
   223      (\<exists> i j k t. i + j = Suc x \<and> i > 0 \<and> j > 0 \<and> k + t = j \<and> t > 0 \<and> (l, r) = (Oc\<up>k@Bk\<up>j@Oc\<up>i, Oc\<up>t))"
   194      (\<exists> i j k t. i + j = Suc x \<and> i > 0 \<and> j > 0 \<and> k + t = j \<and> t > 0 \<and> (l, r) = (Oc\<up>k@Bk\<up>j@Oc\<up>i, Oc\<up>t))"
   224 | "inv_loop5_exit x (l, r) = 
   195 | "inv_loop5_exit x (l, r) = 
   225      (\<exists> i j. i + j = Suc x \<and> i > 0 \<and> j > 0 \<and> (l, r) = (Bk\<up>(j - 1)@Oc\<up>i, Bk # Oc\<up>j))"
   196      (\<exists> i j. i + j = Suc x \<and> i > 0 \<and> j > 0 \<and> (l, r) = (Bk\<up>(j - 1)@Oc\<up>i, Bk # Oc\<up>j))"
   226 | "inv_loop6_loop x (l, r) = 
   197 | "inv_loop6_loop x (l, r) = 
   235   inv_loop3 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   206   inv_loop3 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   236   inv_loop4 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   207   inv_loop4 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   237   inv_loop5 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   208   inv_loop5 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   238   inv_loop6 :: "nat \<Rightarrow> tape \<Rightarrow> bool"
   209   inv_loop6 :: "nat \<Rightarrow> tape \<Rightarrow> bool"
   239 where
   210 where
   240   "inv_loop0 x (l, r) =  ((l, r) = ([Bk], Oc # Bk\<up>x @ Oc\<up>x) \<and> x > 0)"
   211   "inv_loop0 n (l, r) =  (0 < n \<and> (l, r) = ([Bk], Oc # Bk\<up>n @ Oc\<up>n))"
   241 | "inv_loop1 x (l, r) = (inv_loop1_loop x (l, r) \<or> inv_loop1_exit x (l, r))"
   212 | "inv_loop1 n (l, r) = (inv_loop1_loop n (l, r) \<or> inv_loop1_exit n (l, r))"
   242 | "inv_loop2 x (l, r) = (\<exists> i j any. i + j = x \<and> x > 0 \<and> i > 0 \<and> j > 0 \<and> (l, r) = (Oc\<up>i, any#Bk\<up>j@Oc\<up>j))"
   213 | "inv_loop2 n (l, r) = (\<exists> i j any. i + j = n \<and> n > 0 \<and> i > 0 \<and> j > 0 \<and> (l, r) = (Oc\<up>i, any#Bk\<up>j@Oc\<up>j))"
   243 | "inv_loop3 x (l, r) = 
   214 | "inv_loop3 n (l, r) = 
   244      (\<exists> i j k t. i + j = x \<and> i > 0 \<and> j > 0 \<and>  k + t = Suc j \<and> (l, r) = (Bk\<up>k@Oc\<up>i, Bk\<up>t@Oc\<up>j))"
   215      (\<exists> i j k t. i + j = n \<and> i > 0 \<and> j > 0 \<and>  k + t = Suc j \<and> (l, r) = (Bk\<up>k@Oc\<up>i, Bk\<up>t@Oc\<up>j))"
   245 | "inv_loop4 x (l, r) = 
   216 | "inv_loop4 n (l, r) = 
   246      (\<exists> i j k t. i + j = x \<and> i > 0 \<and> j > 0 \<and>  k + t = j \<and> (l, r) = (Oc\<up>k @ Bk\<up>(Suc j)@Oc\<up>i, Oc\<up>t))"
   217      (\<exists> i j k t. i + j = n \<and> i > 0 \<and> j > 0 \<and>  k + t = j \<and> (l, r) = (Oc\<up>k @ Bk\<up>(Suc j)@Oc\<up>i, Oc\<up>t))"
   247 | "inv_loop5 x (l, r) = (inv_loop5_loop x (l, r) \<or> inv_loop5_exit x (l, r))"
   218 | "inv_loop5 n (l, r) = (inv_loop5_loop n (l, r) \<or> inv_loop5_exit n (l, r))"
   248 | "inv_loop6 x (l, r) = (inv_loop6_loop x (l, r) \<or> inv_loop6_exit x (l, r))"
   219 | "inv_loop6 n (l, r) = (inv_loop6_loop n (l, r) \<or> inv_loop6_exit n (l, r))"
   249 
   220 
   250 fun inv_loop :: "nat \<Rightarrow> config \<Rightarrow> bool"
   221 fun inv_loop :: "nat \<Rightarrow> config \<Rightarrow> bool"
   251   where
   222   where
   252   "inv_loop x (s, l, r) = 
   223   "inv_loop x (s, l, r) = 
   253          (if s = 0 then inv_loop0 x (l, r)
   224          (if s = 0 then inv_loop0 x (l, r)
   462 apply(case_tac cf, case_tac c, case_tac [2] aa)
   433 apply(case_tac cf, case_tac c, case_tac [2] aa)
   463 apply(auto simp: inv_loop.simps step.simps tcopy_loop_def numeral split: if_splits)
   434 apply(auto simp: inv_loop.simps step.simps tcopy_loop_def numeral split: if_splits)
   464 done
   435 done
   465 
   436 
   466 lemma inv_loop_steps:
   437 lemma inv_loop_steps:
   467   "\<lbrakk>inv_loop x cf; x > 0\<rbrakk>
   438   "\<lbrakk>inv_loop x cf; x > 0\<rbrakk> \<Longrightarrow> inv_loop x (steps cf (tcopy_loop, 0) stp)"
   468   \<Longrightarrow> inv_loop x (steps cf (tcopy_loop, 0) stp)"
       
   469 apply(induct stp, simp add: steps.simps, simp)
   439 apply(induct stp, simp add: steps.simps, simp)
   470 apply(erule_tac inv_loop_step, simp)
   440 apply(erule_tac inv_loop_step, simp)
   471 done
   441 done
   472 
   442 
   473 fun loop_stage :: "config \<Rightarrow> nat"
   443 fun loop_stage :: "config \<Rightarrow> nat"
   487                           else if s = 4 then length r
   457                           else if s = 4 then length r
   488                           else if s = 5 then length l 
   458                           else if s = 5 then length l 
   489                           else if s = 6 then length l
   459                           else if s = 6 then length l
   490                           else 0)"
   460                           else 0)"
   491 
   461 
   492 definition loop_LE :: "(config \<times> config) set"
   462 definition measure_loop :: "(config \<times> config) set"
   493   where
   463   where
   494    "loop_LE = measures [loop_stage, loop_state, loop_step]"
   464    "measure_loop = measures [loop_stage, loop_state, loop_step]"
   495 
   465 
   496 lemma wf_loop_le: "wf loop_LE"
   466 lemma wf_measure_loop: "wf measure_loop"
   497 unfolding loop_LE_def
   467 unfolding measure_loop_def
   498 by (auto)
   468 by (auto)
       
   469 
       
   470 lemma measure_loop_induct [case_names Step]: 
       
   471   "\<lbrakk>\<And>n. \<not> P (f n) \<Longrightarrow> (f (Suc n), (f n)) \<in> measure_loop\<rbrakk> \<Longrightarrow> \<exists>n. P (f n)"
       
   472 using wf_measure_loop
       
   473 by (metis wf_iff_no_infinite_down_chain)
       
   474 
       
   475 
   499 
   476 
   500 lemma [simp]: "inv_loop2 x ([], b) = False"
   477 lemma [simp]: "inv_loop2 x ([], b) = False"
   501 by (auto simp: inv_loop2.simps)
   478 by (auto simp: inv_loop2.simps)
   502 
   479 
   503 lemma  [simp]: "inv_loop2 x (l', []) = False"
   480 lemma  [simp]: "inv_loop2 x (l', []) = False"
   506 lemma [simp]: "inv_loop3 x (b, []) = False"
   483 lemma [simp]: "inv_loop3 x (b, []) = False"
   507 by (auto simp: inv_loop3.simps)
   484 by (auto simp: inv_loop3.simps)
   508 
   485 
   509 lemma [simp]: "inv_loop4 x ([], b) = False"
   486 lemma [simp]: "inv_loop4 x ([], b) = False"
   510 by (auto simp: inv_loop4.simps)
   487 by (auto simp: inv_loop4.simps)
       
   488 
   511 
   489 
   512 lemma [elim]: 
   490 lemma [elim]: 
   513   "\<lbrakk>inv_loop4 x (l', []); l' \<noteq> []; x > 0;
   491   "\<lbrakk>inv_loop4 x (l', []); l' \<noteq> []; x > 0;
   514   length (takeWhile (\<lambda>a. a = Oc) (rev l' @ [Oc])) \<noteq> 
   492   length (takeWhile (\<lambda>a. a = Oc) (rev l' @ [Oc])) \<noteq> 
   515   length (takeWhile (\<lambda>a. a = Oc) (rev l'))\<rbrakk>
   493   length (takeWhile (\<lambda>a. a = Oc) (rev l'))\<rbrakk>
   579   length (takeWhile (\<lambda>a. a = Oc) (rev l' @ Oc # list))"
   557   length (takeWhile (\<lambda>a. a = Oc) (rev l' @ Oc # list))"
   580 apply(auto simp: inv_loop5.simps)
   558 apply(auto simp: inv_loop5.simps)
   581 apply(case_tac l', auto)
   559 apply(case_tac l', auto)
   582 done
   560 done
   583 
   561 
   584 
       
   585 lemma[elim]: 
   562 lemma[elim]: 
   586   "\<lbrakk>inv_loop6 x (l', Oc # list); l' \<noteq> []; 0 < x;
   563   "\<lbrakk>inv_loop6 x (l', Oc # list); l' \<noteq> []; 0 < x;
   587   length (takeWhile (\<lambda>a. a = Oc) (rev (tl l') @ hd l' # Oc # list))
   564   length (takeWhile (\<lambda>a. a = Oc) (rev (tl l') @ hd l' # Oc # list))
   588   \<noteq> length (takeWhile (\<lambda>a. a = Oc) (rev l' @ Oc # list))\<rbrakk>
   565   \<noteq> length (takeWhile (\<lambda>a. a = Oc) (rev l' @ Oc # list))\<rbrakk>
   589   \<Longrightarrow> length (takeWhile (\<lambda>a. a = Oc) (rev (tl l') @ hd l' # Oc # list)) < 
   566   \<Longrightarrow> length (takeWhile (\<lambda>a. a = Oc) (rev (tl l') @ hd l' # Oc # list)) < 
   590       length (takeWhile (\<lambda>a. a = Oc) (rev l' @ Oc # list))"
   567       length (takeWhile (\<lambda>a. a = Oc) (rev l' @ Oc # list))"
   591 apply(case_tac l')
   568 apply(case_tac l')
   592 apply(auto simp: inv_loop6.simps)
   569 apply(auto simp: inv_loop6.simps)
   593 done
   570 done
   594 
   571 
   595 lemma loop_halt: 
   572 lemma loop_halts: 
   596   "\<lbrakk>x > 0; inv_loop x (Suc 0, l, r)\<rbrakk> \<Longrightarrow> 
   573   assumes h: "n > 0" "inv_loop n (1, l, r)"
   597       \<exists> stp. is_final (steps (Suc 0, l, r) (tcopy_loop, 0) stp)"
   574   shows "\<exists> stp. is_final (steps0 (1, l, r) tcopy_loop stp)"
   598 proof(rule_tac LE = loop_LE in halt_lemma)
   575 proof (induct rule: measure_loop_induct) 
   599   show "wf loop_LE" by(intro wf_loop_le)
   576   case (Step stp)
   600 next
   577   have "\<not> is_final (steps0 (1, l, r) tcopy_loop stp)" by fact
   601   assume h: "0 < x"  and g: "inv_loop x (Suc 0, l, r)"
   578   moreover
   602   show "\<forall>n. \<not> is_final (steps (Suc 0, l, r) (tcopy_loop, 0) n) \<longrightarrow> 
   579   have "inv_loop n (steps0 (1, l, r) tcopy_loop stp)"
   603         (steps (Suc 0, l, r) (tcopy_loop, 0) (Suc n), steps (Suc 0, l, r) (tcopy_loop, 0) n) \<in> loop_LE"
   580     by (rule_tac inv_loop_steps) (simp_all only: h)
   604   proof(rule_tac allI, rule_tac impI)
   581   moreover
   605     fix n
   582   obtain s l' r' where eq: "(steps0 (1, l, r) tcopy_loop stp) = (s, l', r')"
   606     assume a: "\<not> is_final (steps (Suc 0, l, r) (tcopy_loop, 0) n)"
   583     by (metis measure_begin_state.cases)
   607     obtain s' l' r' where d: "(steps (Suc 0, l, r) (tcopy_loop, 0) n) = (s', l', r')"
   584   ultimately 
   608       by(case_tac "(steps (Suc 0, l, r) (tcopy_loop, 0) n)", auto)
   585   have "(step0 (s, l', r') tcopy_loop, s, l', r') \<in> measure_loop"
   609     hence "inv_loop x (s', l', r') \<and> s' \<noteq> 0"
   586     using h(1)
   610       using h g
   587     apply(case_tac r')
   611       apply(drule_tac stp = n in inv_loop_steps, auto)
   588     apply(case_tac [2] a)
   612       using a
   589     apply(auto simp: inv_loop.simps step.simps tcopy_loop_def numeral measure_loop_def split: if_splits)
   613       apply(simp)
   590     done
   614       done
   591   then 
   615     hence "(step(s', l', r') (tcopy_loop, 0), s', l', r') \<in> loop_LE"
   592   show "(steps0 (1, l, r) tcopy_loop (Suc stp), steps0 (1, l, r) tcopy_loop stp) \<in> measure_loop"
   616       using h 
   593     using eq by (simp only: step_red)
   617       apply(case_tac r', case_tac [2] a)
       
   618       apply(auto simp: inv_loop.simps step.simps tcopy_loop_def numeral loop_LE_def split: if_splits)
       
   619       done
       
   620     thus "(steps (Suc 0, l, r) (tcopy_loop, 0) (Suc n), 
       
   621           steps (Suc 0, l, r) (tcopy_loop, 0) n) \<in> loop_LE"
       
   622       using d
       
   623       apply(simp add: step_red)
       
   624       done
       
   625   qed
       
   626 qed
   594 qed
   627 
   595 
   628 lemma loop_correct:
   596 lemma loop_correct:
   629   "x > 0 \<Longrightarrow> {inv_loop1 x} tcopy_loop {inv_loop0 x}"
   597   shows "0 < n \<Longrightarrow> {inv_loop1 n} tcopy_loop {inv_loop0 n}"
       
   598   using assms
   630 proof(rule_tac Hoare_haltI)
   599 proof(rule_tac Hoare_haltI)
   631   fix l r
   600   fix l r
   632   assume h: "0 < x"
   601   assume h: "0 < n" "inv_loop1 n (l, r)"
   633     "inv_loop1 x (l, r)"
   602   then obtain stp where k: "is_final (steps0 (1, l, r) tcopy_loop stp)" 
   634   hence "\<exists> stp. is_final (steps (Suc 0, l, r) (tcopy_loop, 0) stp)"
   603     using loop_halts
   635     apply(rule_tac loop_halt, simp_all add: inv_loop.simps)
   604     apply(simp add: inv_loop.simps)
       
   605     apply(blast)
   636     done
   606     done
   637   then obtain stp where "is_final (steps (Suc 0, l, r) (tcopy_loop, 0) stp)" ..
   607   moreover
   638   moreover have "inv_loop x (steps (Suc 0, l, r) (tcopy_loop, 0) stp)"
   608   have "inv_loop n (steps0 (1, l, r) tcopy_loop stp)"
   639     apply(rule_tac inv_loop_steps)
   609     using h 
   640     using h by(simp_all add: inv_loop.simps)
   610     by (rule_tac inv_loop_steps) (simp_all add: inv_loop.simps)
   641   ultimately show
   611   ultimately show
   642     "\<exists>n. is_final (steps (1, l, r) (tcopy_loop, 0) n) \<and> 
   612     "\<exists>stp. is_final (steps0 (1, l, r) tcopy_loop stp) \<and> 
   643     inv_loop0 x holds_for steps (1, l, r) (tcopy_loop, 0) n"    
   613     inv_loop0 n holds_for steps0 (1, l, r) tcopy_loop stp"
   644     using h
   614     using h(1) 
   645     apply(rule_tac x = stp in exI)
   615     apply(rule_tac x = stp in exI)
   646     apply(case_tac "(steps (Suc 0, l, r) (tcopy_loop, 0) stp)", 
   616     apply(case_tac "(steps0 (1, l, r) tcopy_loop stp)")
   647       simp add: inv_begin.simps inv_loop.simps)
   617     apply(simp add: inv_loop.simps)
   648     done
   618     done
   649 qed
   619 qed
       
   620 
       
   621 
   650 
   622 
   651 
   623 
   652 (* tcopy_end *)
   624 (* tcopy_end *)
   653 
   625 
   654 fun
   626 fun
   662 fun 
   634 fun 
   663   inv_end0 :: "nat \<Rightarrow> tape \<Rightarrow>  bool" and
   635   inv_end0 :: "nat \<Rightarrow> tape \<Rightarrow>  bool" and
   664   inv_end1 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   636   inv_end1 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   665   inv_end2 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   637   inv_end2 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   666   inv_end3 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   638   inv_end3 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   667   inv_end4 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
   639   inv_end4 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and 
   668  
       
   669   inv_end5 :: "nat \<Rightarrow> tape \<Rightarrow> bool" 
   640   inv_end5 :: "nat \<Rightarrow> tape \<Rightarrow> bool" 
   670 where
   641 where
   671   "inv_end0 x (l, r) = (x > 0 \<and> l = [Bk] \<and> r = Oc\<up>x @ Bk # Oc\<up>x)"
   642   "inv_end0 n (l, r) = (n > 0 \<and> (l, r) = ([Bk], Oc\<up>n @ Bk # Oc\<up>n))"
   672 | "inv_end1 x (l, r) = (x > 0 \<and> l = [Bk] \<and> r = Oc # Bk\<up>x @ Oc\<up>x)"
   643 | "inv_end1 n (l, r) = (n > 0 \<and> (l, r) = ([Bk], Oc # Bk\<up>n @ Oc\<up>n))"
   673 | "inv_end2 x (l, r) = (\<exists> i j. i + j = Suc x \<and> x > 0 \<and> l = Oc\<up>i @ [Bk] \<and> r = Bk\<up>j @ Oc\<up>x)"
   644 | "inv_end2 n (l, r) = (\<exists> i j. i + j = Suc n \<and> n > 0 \<and> l = Oc\<up>i @ [Bk] \<and> r = Bk\<up>j @ Oc\<up>n)"
   674 | "inv_end3 x (l, r) =
   645 | "inv_end3 n (l, r) =
   675      (\<exists> i j. x > 0 \<and> i + j = x \<and> l = Oc\<up>i @ [Bk] \<and> r = Oc # Bk\<up>j@ Oc\<up>x)"
   646      (\<exists> i j. n > 0 \<and> i + j = n \<and> l = Oc\<up>i @ [Bk] \<and> r = Oc # Bk\<up>j@ Oc\<up>n)"
   676 | "inv_end4 x (l, r) = (\<exists> any. x > 0 \<and> l = Oc\<up>x @ [Bk] \<and> r = any#Oc\<up>x)"
   647 | "inv_end4 n (l, r) = (\<exists> any. n > 0 \<and> l = Oc\<up>n @ [Bk] \<and> r = any#Oc\<up>n)"
   677 | "inv_end5 x (l, r) = (inv_end5_loop x (l, r) \<or> inv_end5_exit x (l, r))"
   648 | "inv_end5 n (l, r) = (inv_end5_loop n (l, r) \<or> inv_end5_exit n (l, r))"
   678 
   649 
   679 fun 
   650 fun 
   680   inv_end :: "nat \<Rightarrow> config \<Rightarrow> bool"
   651   inv_end :: "nat \<Rightarrow> config \<Rightarrow> bool"
   681 where
   652 where
   682   "inv_end x (s, l, r) = (if s = 0 then inv_end0 x (l, r)
   653   "inv_end n (s, l, r) = (if s = 0 then inv_end0 n (l, r)
   683                           else if s = 1 then inv_end1 x (l, r)
   654                           else if s = 1 then inv_end1 n (l, r)
   684                           else if s = 2 then inv_end2 x (l, r)
   655                           else if s = 2 then inv_end2 n (l, r)
   685                           else if s = 3 then inv_end3 x (l, r)
   656                           else if s = 3 then inv_end3 n (l, r)
   686                           else if s = 4 then inv_end4 x (l, r)
   657                           else if s = 4 then inv_end4 n (l, r)
   687                           else if s = 5 then inv_end5 x (l, r)
   658                           else if s = 5 then inv_end5 n (l, r)
   688                           else False)"
   659                           else False)"
   689 
   660 
   690 declare inv_end.simps[simp del] inv_end1.simps[simp del]
   661 declare inv_end.simps[simp del] inv_end1.simps[simp del]
   691         inv_end0.simps[simp del] inv_end2.simps[simp del]
   662         inv_end0.simps[simp del] inv_end2.simps[simp del]
   692         inv_end3.simps[simp del] inv_end4.simps[simp del]
   663         inv_end3.simps[simp del] inv_end4.simps[simp del]
   840 unfolding end_LE_def
   811 unfolding end_LE_def
   841 by (auto)
   812 by (auto)
   842 
   813 
   843 lemma [simp]: "inv_end5 x ([], Oc # list) = False"
   814 lemma [simp]: "inv_end5 x ([], Oc # list) = False"
   844 by (auto simp: inv_end5.simps inv_end5_loop.simps)
   815 by (auto simp: inv_end5.simps inv_end5_loop.simps)
       
   816 
       
   817 lemma halt_lemma: 
       
   818   "\<lbrakk>wf LE; \<forall>n. (\<not> P (f n) \<longrightarrow> (f (Suc n), (f n)) \<in> LE)\<rbrakk> \<Longrightarrow> \<exists>n. P (f n)"
       
   819 by (metis wf_iff_no_infinite_down_chain)
   845 
   820 
   846 lemma end_halt: 
   821 lemma end_halt: 
   847   "\<lbrakk>x > 0; inv_end x (Suc 0, l, r)\<rbrakk> \<Longrightarrow> 
   822   "\<lbrakk>x > 0; inv_end x (Suc 0, l, r)\<rbrakk> \<Longrightarrow> 
   848       \<exists> stp. is_final (steps (Suc 0, l, r) (tcopy_end, 0) stp)"
   823       \<exists> stp. is_final (steps (Suc 0, l, r) (tcopy_end, 0) stp)"
   849 proof(rule_tac LE = end_LE in halt_lemma)
   824 proof(rule_tac LE = end_LE in halt_lemma)
   873       by simp
   848       by simp
   874   qed
   849   qed
   875 qed
   850 qed
   876 
   851 
   877 lemma end_correct:
   852 lemma end_correct:
   878   "x > 0 \<Longrightarrow> {inv_end1 x} tcopy_end {inv_end0 x}"
   853   "n > 0 \<Longrightarrow> {inv_end1 n} tcopy_end {inv_end0 n}"
   879 proof(rule_tac Hoare_haltI)
   854 proof(rule_tac Hoare_haltI)
   880   fix l r
   855   fix l r
   881   assume h: "0 < x"
   856   assume h: "0 < n"
   882     "inv_end1 x (l, r)"
   857     "inv_end1 n (l, r)"
   883   then have "\<exists> stp. is_final (steps0 (1, l, r) tcopy_end stp)"
   858   then have "\<exists> stp. is_final (steps0 (1, l, r) tcopy_end stp)"
   884     by (simp add: end_halt inv_end.simps)
   859     by (simp add: end_halt inv_end.simps)
   885   then obtain stp where "is_final (steps0 (1, l, r) tcopy_end stp)" ..
   860   then obtain stp where "is_final (steps0 (1, l, r) tcopy_end stp)" ..
   886   moreover have "inv_end x (steps0 (1, l, r) tcopy_end stp)"
   861   moreover have "inv_end n (steps0 (1, l, r) tcopy_end stp)"
   887     apply(rule_tac inv_end_steps)
   862     apply(rule_tac inv_end_steps)
   888     using h by(simp_all add: inv_end.simps)
   863     using h by(simp_all add: inv_end.simps)
   889   ultimately show
   864   ultimately show
   890     "\<exists>n. is_final (steps (1, l, r) (tcopy_end, 0) n) \<and> 
   865     "\<exists>stp. is_final (steps (1, l, r) (tcopy_end, 0) stp) \<and> 
   891     inv_end0 x holds_for steps (1, l, r) (tcopy_end, 0) n"        
   866     inv_end0 n holds_for steps (1, l, r) (tcopy_end, 0) stp"        
   892     using h
   867     using h
   893     apply(rule_tac x = stp in exI)
   868     apply(rule_tac x = stp in exI)
   894     apply(case_tac "(steps0 (1, l, r) tcopy_end stp)") 
   869     apply(case_tac "(steps0 (1, l, r) tcopy_end stp)") 
   895     apply(simp add: inv_end.simps)
   870     apply(simp add: inv_end.simps)
   896     done
   871     done
   938   show "{inv_begin1 x} tcopy {inv_end0 x}"
   913   show "{inv_begin1 x} tcopy {inv_end0 x}"
   939     unfolding tcopy_def
   914     unfolding tcopy_def
   940     by (rule_tac Hoare_plus_halt) (auto)
   915     by (rule_tac Hoare_plus_halt) (auto)
   941 qed
   916 qed
   942 
   917 
   943 abbreviation
   918 abbreviation (input)
   944   "pre_tcopy n \<equiv> \<lambda>tp. tp = ([]::cell list, <[n::nat]>)"
   919   "pre_tcopy n \<equiv> \<lambda>tp. tp = ([]::cell list, <(n::nat)>)"
   945 abbreviation
   920 abbreviation (input)
   946   "post_tcopy n \<equiv> \<lambda>tp. tp= ([Bk], <[n, n::nat]>)"
   921   "post_tcopy n \<equiv> \<lambda>tp. tp= ([Bk], <(n, n::nat)>)"
   947 
   922 
   948 lemma tcopy_correct:
   923 lemma tcopy_correct:
   949   shows "{pre_tcopy n} tcopy {post_tcopy n}"
   924   shows "{pre_tcopy n} tcopy {post_tcopy n}"
   950 proof -
   925 proof -
   951   have "{inv_begin1 (Suc n)} tcopy {inv_end0 (Suc n)}"
   926   have "{inv_begin1 (Suc n)} tcopy {inv_end0 (Suc n)}"
   953   moreover
   928   moreover
   954   have "pre_tcopy n = inv_begin1 (Suc n)" 
   929   have "pre_tcopy n = inv_begin1 (Suc n)" 
   955     by (auto simp add: tape_of_nl_abv tape_of_nat_abv)
   930     by (auto simp add: tape_of_nl_abv tape_of_nat_abv)
   956   moreover
   931   moreover
   957   have "inv_end0 (Suc n) = post_tcopy n" 
   932   have "inv_end0 (Suc n) = post_tcopy n" 
   958     by (auto simp add: inv_end0.simps tape_of_nat_abv tape_of_nl_abv)
   933     by (auto simp add: inv_end0.simps tape_of_nat_abv tape_of_nat_pair)
   959   ultimately
   934   ultimately
   960   show "{pre_tcopy n} tcopy {post_tcopy n}" 
   935   show "{pre_tcopy n} tcopy {post_tcopy n}" 
   961     by simp
   936     by simp
   962 qed
   937 qed
   963 
   938 
  1042   assumes h_wf[intro]: "tm_wf0 H"
  1017   assumes h_wf[intro]: "tm_wf0 H"
  1043   -- {*
  1018   -- {*
  1044   The following two assumptions specifies that @{text "H"} does solve the Halting problem.
  1019   The following two assumptions specifies that @{text "H"} does solve the Halting problem.
  1045   *}
  1020   *}
  1046   and h_case: 
  1021   and h_case: 
  1047   "\<And> M ns. haltP M ns \<Longrightarrow> {(\<lambda>tp. tp = ([Bk], <code M#ns>))} H {(\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>))}"
  1022   "\<And> M ns. haltP M ns \<Longrightarrow> {(\<lambda>tp. tp = ([Bk], <(code M, ns)>))} H {(\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>))}"
  1048   and nh_case: 
  1023   and nh_case: 
  1049   "\<And> M ns. \<not> haltP M ns \<Longrightarrow> {(\<lambda>tp. tp = ([Bk], <code M#ns>))} H {(\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>))}"
  1024   "\<And> M ns. \<not> haltP M ns \<Longrightarrow> {(\<lambda>tp. tp = ([Bk], <(code M, ns)>))} H {(\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>))}"
  1050 begin
  1025 begin
  1051 
  1026 
  1052 (* invariants for H *)
  1027 (* invariants for H *)
  1053 abbreviation
  1028 abbreviation (input)
  1054   "pre_H_inv M n \<equiv> \<lambda>tp. tp = ([Bk], <[code M, n]>)"
  1029   "pre_H_inv M ns \<equiv> \<lambda>tp. tp = ([Bk], <(code M, ns::nat list)>)"
  1055 
  1030 
  1056 abbreviation
  1031 abbreviation (input)
  1057   "post_H_halt_inv \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>)"
  1032   "post_H_halt_inv \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>)"
  1058 
  1033 
  1059 abbreviation
  1034 abbreviation (input)
  1060   "post_H_unhalt_inv \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>)"
  1035   "post_H_unhalt_inv \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>)"
  1061 
  1036 
  1062 
  1037 
  1063 lemma H_halt_inv:
  1038 lemma H_halt_inv:
  1064   assumes "\<not> haltP M [n]" 
  1039   assumes "\<not> haltP M ns" 
  1065   shows "{pre_H_inv M n} H {post_H_halt_inv}"
  1040   shows "{pre_H_inv M ns} H {post_H_halt_inv}"
  1066 using assms nh_case by auto
  1041 using assms nh_case by auto
  1067 
  1042 
  1068 lemma H_unhalt_inv:
  1043 lemma H_unhalt_inv:
  1069   assumes "haltP M [n]" 
  1044   assumes "haltP M ns" 
  1070   shows "{pre_H_inv M n} H {post_H_unhalt_inv}"
  1045   shows "{pre_H_inv M ns} H {post_H_unhalt_inv}"
  1071 using assms h_case by auto
  1046 using assms h_case by auto
  1072    
  1047    
  1073 (* TM that produces the contradiction and its code *)
  1048 (* TM that produces the contradiction and its code *)
  1074 
  1049 
  1075 definition
  1050 definition
  1081 lemma tcontra_unhalt: 
  1056 lemma tcontra_unhalt: 
  1082   assumes "\<not> haltP tcontra [code tcontra]"
  1057   assumes "\<not> haltP tcontra [code tcontra]"
  1083   shows "False"
  1058   shows "False"
  1084 proof -
  1059 proof -
  1085   (* invariants *)
  1060   (* invariants *)
  1086   def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <[code_tcontra]>)"
  1061   def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <code_tcontra>)"
  1087   def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <[code_tcontra, code_tcontra]>)"
  1062   def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <(code_tcontra, code_tcontra)>)"
  1088   def P3 \<equiv> "\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>)"
  1063   def P3 \<equiv> "\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>)"
  1089 
  1064 
  1090   (*
  1065   (*
  1091   {P1} tcopy {P2}  {P2} H {P3} 
  1066   {P1} tcopy {P2}  {P2} H {P3} 
  1092   ----------------------------
  1067   ----------------------------
  1104     show "{P1} tcopy {P2}" unfolding P1_def P2_def 
  1079     show "{P1} tcopy {P2}" unfolding P1_def P2_def 
  1105       by (rule tcopy_correct)
  1080       by (rule tcopy_correct)
  1106   next
  1081   next
  1107     case B_halt (* of H *)
  1082     case B_halt (* of H *)
  1108     show "{P2} H {P3}"
  1083     show "{P2} H {P3}"
  1109       unfolding P2_def P3_def
  1084       unfolding P2_def P3_def 
  1110       by (rule H_halt_inv[OF assms])
  1085       using H_halt_inv[OF assms]
       
  1086       by (simp add: tape_of_nat_pair tape_of_nl_abv)
  1111   qed (simp)
  1087   qed (simp)
  1112 
  1088 
  1113   (* {P3} dither {P3} *)
  1089   (* {P3} dither {P3} *)
  1114   have second: "{P3} dither {P3}" unfolding P3_def 
  1090   have second: "{P3} dither {P3}" unfolding P3_def 
  1115     by (rule dither_halts)
  1091     by (rule dither_halts)
  1121 
  1097 
  1122   with assms show "False"
  1098   with assms show "False"
  1123     unfolding P1_def P3_def
  1099     unfolding P1_def P3_def
  1124     unfolding haltP_def
  1100     unfolding haltP_def
  1125     unfolding Hoare_halt_def 
  1101     unfolding Hoare_halt_def 
  1126     apply(auto)
  1102     apply(auto)    
  1127     apply(drule_tac x = n in spec)
  1103     apply(drule_tac x = n in spec)
  1128     apply(case_tac "steps0 (Suc 0, [], <[code tcontra]>) tcontra n")
  1104     apply(case_tac "steps0 (Suc 0, [], <code tcontra>) tcontra n")
  1129     apply(auto)
  1105     apply(auto simp add: tape_of_nl_abv)
  1130     done
  1106     done
  1131 qed
  1107 qed
  1132 
  1108 
  1133 (* asumme tcontra halts on its code *)
  1109 (* asumme tcontra halts on its code *)
  1134 lemma tcontra_halt: 
  1110 lemma tcontra_halt: 
  1135   assumes "haltP tcontra [code tcontra]"
  1111   assumes "haltP tcontra [code tcontra]"
  1136   shows "False"
  1112   shows "False"
  1137 proof - 
  1113 proof - 
  1138   (* invariants *)
  1114   (* invariants *)
  1139   def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <[code_tcontra]>)"
  1115   def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <code_tcontra>)"
  1140   def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <[code_tcontra, code_tcontra]>)"
  1116   def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <(code_tcontra, code_tcontra)>)"
  1141   def Q3 \<equiv> "\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>)"
  1117   def Q3 \<equiv> "\<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>)"
  1142 
  1118 
  1143   (*
  1119   (*
  1144   {P1} tcopy {P2}  {P2} H {Q3} 
  1120   {P1} tcopy {P2}  {P2} H {Q3} 
  1145   ----------------------------
  1121   ----------------------------
  1157     show "{P1} tcopy {P2}" unfolding P1_def P2_def
  1133     show "{P1} tcopy {P2}" unfolding P1_def P2_def
  1158       by (rule tcopy_correct)
  1134       by (rule tcopy_correct)
  1159   next
  1135   next
  1160     case B_halt (* of H *)
  1136     case B_halt (* of H *)
  1161     then show "{P2} H {Q3}"
  1137     then show "{P2} H {Q3}"
  1162       unfolding P2_def Q3_def
  1138       unfolding P2_def Q3_def using H_unhalt_inv[OF assms]
  1163       by (rule H_unhalt_inv[OF assms])
  1139       by(simp add: tape_of_nat_pair tape_of_nl_abv)
  1164   qed (simp)
  1140   qed (simp)
  1165 
  1141 
  1166   (* {P3} dither loops *)
  1142   (* {P3} dither loops *)
  1167   have second: "{Q3} dither \<up>" unfolding Q3_def 
  1143   have second: "{Q3} dither \<up>" unfolding Q3_def 
  1168     by (rule dither_loops)
  1144     by (rule dither_loops)
  1174 
  1150 
  1175   with assms show "False"
  1151   with assms show "False"
  1176     unfolding P1_def
  1152     unfolding P1_def
  1177     unfolding haltP_def
  1153     unfolding haltP_def
  1178     unfolding Hoare_halt_def Hoare_unhalt_def
  1154     unfolding Hoare_halt_def Hoare_unhalt_def
  1179     by auto
  1155     by (auto simp add: tape_of_nl_abv)
  1180 qed
  1156 qed
  1181       
  1157       
  1182 text {*
  1158 text {*
  1183   @{text "False"} can finally derived.
  1159   @{text "False"} can finally derived.
  1184 *}
  1160 *}