thys/uncomputable.thy
changeset 89 c67e8ed4c865
parent 84 4c8325c64dab
child 91 2068654bdf54
equal deleted inserted replaced
88:904f9351ab94 89:c67e8ed4c865
    28 text {*
    28 text {*
    29   The {\em Copying} TM, which duplicates its input. 
    29   The {\em Copying} TM, which duplicates its input. 
    30 *}
    30 *}
    31 
    31 
    32 definition 
    32 definition 
    33   tcopy_init :: "instr list"
    33   tcopy_begin :: "instr list"
    34 where
    34 where
    35   "tcopy_init \<equiv> [(W0, 0), (R, 2), (R, 3), (R, 2),
    35   "tcopy_begin \<equiv> [(W0, 0), (R, 2), (R, 3), (R, 2),
    36                  (W1, 3), (L, 4), (L, 4), (L, 0)]"
    36                  (W1, 3), (L, 4), (L, 4), (L, 0)]"
    37 
    37 
    38 definition 
    38 definition 
    39   tcopy_loop :: "instr list"
    39   tcopy_loop :: "instr list"
    40 where
    40 where
    50                 (R, 0), (L, 5)]"
    50                 (R, 0), (L, 5)]"
    51 
    51 
    52 definition 
    52 definition 
    53   tcopy :: "instr list"
    53   tcopy :: "instr list"
    54 where
    54 where
    55   "tcopy \<equiv> (tcopy_init |+| tcopy_loop) |+| tcopy_end"
    55   "tcopy \<equiv> (tcopy_begin |+| tcopy_loop) |+| tcopy_end"
    56 
    56 
    57 
    57 
    58 (* tcopy_init *)
    58 (* tcopy_begin *)
    59 
    59 
    60 fun 
    60 fun 
    61   inv_init0 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    61   inv_begin0 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    62   inv_init1 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    62   inv_begin1 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    63   inv_init2 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    63   inv_begin2 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    64   inv_init3 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    64   inv_begin3 :: "nat \<Rightarrow> tape \<Rightarrow> bool" and
    65   inv_init4 :: "nat \<Rightarrow> tape \<Rightarrow> bool"
    65   inv_begin4 :: "nat \<Rightarrow> tape \<Rightarrow> bool"
    66 where
    66 where
    67   "inv_init0 n (l, r) = ((n > 1 \<and> (l, r) = (Oc \<up> (n - 2), [Oc, Oc, Bk, Oc])) \<or>   
    67   "inv_begin0 n (l, r) = ((n > 1 \<and> (l, r) = (Oc \<up> (n - 2), [Oc, Oc, Bk, Oc])) \<or>   
    68                          (n = 1 \<and> (l, r) = ([], [Bk, Oc, Bk, Oc])))"
    68                          (n = 1 \<and> (l, r) = ([], [Bk, Oc, Bk, Oc])))"
    69 | "inv_init1 n (l, r) = ((l, r) = ([], Oc \<up> n))"
    69 | "inv_begin1 n (l, r) = ((l, r) = ([], Oc \<up> n))"
    70 | "inv_init2 n (l, r) = (\<exists> i j. i > 0 \<and> i + j = n \<and> (l, r) = (Oc \<up> i, Oc \<up> j))"
    70 | "inv_begin2 n (l, r) = (\<exists> i j. i > 0 \<and> i + j = n \<and> (l, r) = (Oc \<up> i, Oc \<up> j))"
    71 | "inv_init3 n (l, r) = (n > 0 \<and> (l, tl r) = (Bk # Oc \<up> n, []))"
    71 | "inv_begin3 n (l, r) = (n > 0 \<and> (l, tl r) = (Bk # Oc \<up> n, []))"
    72 | "inv_init4 n (l, r) = (n > 0 \<and> ((l, r) = (Oc \<up> n, [Bk, Oc]) \<or> (l, r) = (Oc \<up> (n - 1), [Oc, Bk, Oc])))"
    72 | "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])))"
    73 
    73 
    74 fun inv_init :: "nat \<Rightarrow> config \<Rightarrow> bool"
    74 fun inv_begin :: "nat \<Rightarrow> config \<Rightarrow> bool"
    75   where
    75   where
    76   "inv_init n (s, l, r) = 
    76   "inv_begin n (s, l, r) = 
    77         (if s = 0 then inv_init0 n (l, r) else
    77         (if s = 0 then inv_begin0 n (l, r) else
    78          if s = 1 then inv_init1 n (l, r) else
    78          if s = 1 then inv_begin1 n (l, r) else
    79          if s = 2 then inv_init2 n (l, r) else
    79          if s = 2 then inv_begin2 n (l, r) else
    80          if s = 3 then inv_init3 n (l, r) else
    80          if s = 3 then inv_begin3 n (l, r) else
    81          if s = 4 then inv_init4 n (l, r) 
    81          if s = 4 then inv_begin4 n (l, r) 
    82          else False)"
    82          else False)"
    83 
    83 
    84 
    84 
    85 
    85 
    86 lemma [elim]: "\<lbrakk>0 < i; 0 < j\<rbrakk> \<Longrightarrow> 
    86 lemma [elim]: "\<lbrakk>0 < i; 0 < j\<rbrakk> \<Longrightarrow> 
    87   \<exists>ia>0. ia + j - Suc 0 = i + j \<and> Oc # Oc \<up> i = Oc \<up> ia"
    87   \<exists>ia>0. ia + j - Suc 0 = i + j \<and> Oc # Oc \<up> i = Oc \<up> ia"
    88 apply(rule_tac x = "Suc i" in exI, simp)
    88 apply(rule_tac x = "Suc i" in exI, simp)
    89 done
    89 done
    90 
    90 
    91 lemma inv_init_step: 
    91 lemma inv_begin_step: 
    92   "\<lbrakk>inv_init x cf; x > 0\<rbrakk>
    92   "\<lbrakk>inv_begin x cf; x > 0\<rbrakk>
    93  \<Longrightarrow> inv_init x (step cf (tcopy_init, 0))"
    93  \<Longrightarrow> inv_begin x (step cf (tcopy_begin, 0))"
    94 unfolding tcopy_init_def
    94 unfolding tcopy_begin_def
    95 apply(case_tac cf)
    95 apply(case_tac cf)
    96 apply(auto simp: inv_init.simps step.simps tcopy_init_def numeral split: if_splits)
    96 apply(auto simp: inv_begin.simps step.simps tcopy_begin_def numeral split: if_splits)
    97 apply(case_tac "hd c", auto simp: inv_init.simps)
    97 apply(case_tac "hd c", auto simp: inv_begin.simps)
    98 apply(case_tac c, simp_all)
    98 apply(case_tac c, simp_all)
    99 done
    99 done
   100 
   100 
   101 lemma inv_init_steps: 
   101 lemma inv_begin_steps: 
   102   "\<lbrakk>inv_init x (s, l, r); x > 0\<rbrakk>
   102   "\<lbrakk>inv_begin x (s, l, r); x > 0\<rbrakk>
   103  \<Longrightarrow> inv_init x (steps (s, l, r) (tcopy_init, 0) stp)"
   103  \<Longrightarrow> inv_begin x (steps (s, l, r) (tcopy_begin, 0) stp)"
   104 apply(induct stp, simp add: steps.simps)
   104 apply(induct stp, simp add: steps.simps)
   105 apply(auto simp: step_red)
   105 apply(auto simp: step_red)
   106 apply(rule_tac inv_init_step, simp_all)
   106 apply(rule_tac inv_begin_step, simp_all)
   107 done
   107 done
   108 
   108 
   109 fun init_state :: "config \<Rightarrow> nat"
   109 fun init_state :: "config \<Rightarrow> nat"
   110   where
   110   where
   111   "init_state (s, l, r) = (if s = 0 then 0 else 5 - s)"
   111   "init_state (s, l, r) = (if s = 0 then 0 else 5 - s)"
   133 
   133 
   134 lemma [simp]: "\<lbrakk>tl r = []; r \<noteq> []; r \<noteq> [Bk]\<rbrakk> \<Longrightarrow> r = [Oc]"
   134 lemma [simp]: "\<lbrakk>tl r = []; r \<noteq> []; r \<noteq> [Bk]\<rbrakk> \<Longrightarrow> r = [Oc]"
   135 by (case_tac r, auto, case_tac a, auto)
   135 by (case_tac r, auto, case_tac a, auto)
   136 
   136 
   137 
   137 
   138 lemma wf_init_le: "wf init_LE"
   138 lemma wf_begin_le: "wf init_LE"
   139 by(auto intro:wf_inv_image simp:init_LE_def lex_pair_def)
   139 by(auto intro:wf_inv_image simp:init_LE_def lex_pair_def)
   140 
   140 
   141 lemma init_halt: 
   141 lemma init_halt: 
   142   "x > 0 \<Longrightarrow> \<exists> stp. is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_init, 0) stp)"
   142   "x > 0 \<Longrightarrow> \<exists> stp. is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)"
   143 proof(rule_tac LE = init_LE in halt_lemma)
   143 proof(rule_tac LE = init_LE in halt_lemma)
   144   show "wf init_LE" by(simp add: wf_init_le)
   144   show "wf init_LE" by(simp add: wf_begin_le)
   145 next
   145 next
   146   assume h: "0 < x"
   146   assume h: "0 < x"
   147   show "\<forall>n. \<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n) \<longrightarrow>
   147   show "\<forall>n. \<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<longrightarrow>
   148         (steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) (Suc n), 
   148         (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), 
   149             steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n) \<in> init_LE"
   149             steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> init_LE"
   150   proof(rule_tac allI, rule_tac impI)
   150   proof(rule_tac allI, rule_tac impI)
   151     fix n
   151     fix n
   152     assume a: "\<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n)"
   152     assume a: "\<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n)"
   153     have b: "inv_init x (steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n)"
   153     have b: "inv_begin x (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n)"
   154       apply(rule_tac inv_init_steps)
   154       apply(rule_tac inv_begin_steps)
   155       apply(simp_all add: inv_init.simps h)
   155       apply(simp_all add: inv_begin.simps h)
   156       done 
   156       done 
   157     obtain s l r where c: "(steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n) = (s, l, r)"
   157     obtain s l r where c: "(steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) = (s, l, r)"
   158       apply(case_tac "steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n", auto)
   158       apply(case_tac "steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n", auto)
   159       done
   159       done
   160     moreover hence "inv_init x (s, l, r)" 
   160     moreover hence "inv_begin x (s, l, r)" 
   161       using c b by simp
   161       using c b by simp
   162     ultimately show "(steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) (Suc n), 
   162     ultimately show "(steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), 
   163       steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) n) \<in> init_LE"
   163       steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> init_LE"
   164       using a 
   164       using a 
   165     proof(simp del: inv_init.simps)
   165     proof(simp del: inv_begin.simps)
   166       assume "inv_init x (s, l, r)" "0 < s"
   166       assume "inv_begin x (s, l, r)" "0 < s"
   167       thus "(step (s, l, r) (tcopy_init, 0), s, l, r) \<in> init_LE"
   167       thus "(step (s, l, r) (tcopy_begin, 0), s, l, r) \<in> init_LE"
   168         apply(auto simp: init_LE_def lex_pair_def step.simps tcopy_init_def numeral split: if_splits)
   168         apply(auto simp: init_LE_def lex_pair_def step.simps tcopy_begin_def numeral split: if_splits)
   169         apply(case_tac r, auto, case_tac a, auto)
   169         apply(case_tac r, auto, case_tac a, auto)
   170         done
   170         done
   171     qed
   171     qed
   172   qed
   172   qed
   173 qed
   173 qed
   174     
   174     
   175 lemma init_correct: 
   175 lemma init_correct: 
   176   "x > 0 \<Longrightarrow> {inv_init1 x} tcopy_init {inv_init0 x}"
   176   "x > 0 \<Longrightarrow> {inv_begin1 x} tcopy_begin {inv_begin0 x}"
   177 proof(rule_tac Hoare_haltI)
   177 proof(rule_tac Hoare_haltI)
   178   fix l r
   178   fix l r
   179   assume h: "0 < x"
   179   assume h: "0 < x"
   180     "inv_init1 x (l, r)"
   180     "inv_begin1 x (l, r)"
   181   hence "\<exists> stp. is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_init, 0) stp)"
   181   hence "\<exists> stp. is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)"
   182     by(erule_tac init_halt)    
   182     by(erule_tac init_halt)    
   183   then obtain stp where "is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_init, 0) stp)" ..
   183   then obtain stp where "is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)" ..
   184   moreover have "inv_init x (steps (Suc 0, [], Oc\<up>x) (tcopy_init, 0) stp)"
   184   moreover have "inv_begin x (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)"
   185     apply(rule_tac inv_init_steps)
   185     apply(rule_tac inv_begin_steps)
   186     using h by(simp_all add: inv_init.simps)
   186     using h by(simp_all add: inv_begin.simps)
   187   ultimately show
   187   ultimately show
   188     "\<exists>n. is_final (steps (1, l, r) (tcopy_init, 0) n) \<and> 
   188     "\<exists>n. is_final (steps (1, l, r) (tcopy_begin, 0) n) \<and> 
   189     inv_init0 x holds_for steps (1, l, r) (tcopy_init, 0) n"
   189     inv_begin0 x holds_for steps (1, l, r) (tcopy_begin, 0) n"
   190     using h
   190     using h
   191     apply(rule_tac x = stp in exI)
   191     apply(rule_tac x = stp in exI)
   192     apply(case_tac "(steps (Suc 0, [], Oc \<up> x) (tcopy_init, 0) stp)", simp add: inv_init.simps)
   192     apply(case_tac "(steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) stp)", simp add: inv_begin.simps)
   193     done
   193     done
   194 qed
   194 qed
   195 
   195 
   196 
   196 
   197 (* tcopy_loop *)
   197 (* tcopy_loop *)
   642     "\<exists>n. is_final (steps (1, l, r) (tcopy_loop, 0) n) \<and> 
   642     "\<exists>n. is_final (steps (1, l, r) (tcopy_loop, 0) n) \<and> 
   643     inv_loop0 x holds_for steps (1, l, r) (tcopy_loop, 0) n"    
   643     inv_loop0 x holds_for steps (1, l, r) (tcopy_loop, 0) n"    
   644     using h
   644     using h
   645     apply(rule_tac x = stp in exI)
   645     apply(rule_tac x = stp in exI)
   646     apply(case_tac "(steps (Suc 0, l, r) (tcopy_loop, 0) stp)", 
   646     apply(case_tac "(steps (Suc 0, l, r) (tcopy_loop, 0) stp)", 
   647       simp add: inv_init.simps inv_loop.simps)
   647       simp add: inv_begin.simps inv_loop.simps)
   648     done
   648     done
   649 qed
   649 qed
   650 
   650 
   651 
   651 
   652 (* tcopy_end *)
   652 (* tcopy_end *)
   902     done
   902     done
   903 qed
   903 qed
   904 
   904 
   905 (* tcopy *)
   905 (* tcopy *)
   906 
   906 
   907 lemma [intro]: "tm_wf (tcopy_init, 0)"
   907 lemma [intro]: "tm_wf (tcopy_begin, 0)"
   908 by (auto simp: tm_wf.simps tcopy_init_def)
   908 by (auto simp: tm_wf.simps tcopy_begin_def)
   909 
   909 
   910 lemma [intro]: "tm_wf (tcopy_loop, 0)"
   910 lemma [intro]: "tm_wf (tcopy_loop, 0)"
   911 by (auto simp: tm_wf.simps tcopy_loop_def)
   911 by (auto simp: tm_wf.simps tcopy_loop_def)
   912 
   912 
   913 lemma [intro]: "tm_wf (tcopy_end, 0)"
   913 lemma [intro]: "tm_wf (tcopy_end, 0)"
   915 
   915 
   916 lemma [intro]: "\<lbrakk>tm_wf (A, 0); tm_wf (B, 0)\<rbrakk> \<Longrightarrow> tm_wf (A |+| B, 0)"
   916 lemma [intro]: "\<lbrakk>tm_wf (A, 0); tm_wf (B, 0)\<rbrakk> \<Longrightarrow> tm_wf (A |+| B, 0)"
   917 by (auto simp: tm_wf.simps shift.simps adjust.simps tm_comp_length tm_comp.simps)
   917 by (auto simp: tm_wf.simps shift.simps adjust.simps tm_comp_length tm_comp.simps)
   918 
   918 
   919 lemma tcopy_correct1: 
   919 lemma tcopy_correct1: 
   920   "\<lbrakk>x > 0\<rbrakk> \<Longrightarrow> {inv_init1 x} tcopy {inv_end0 x}"
   920   "\<lbrakk>x > 0\<rbrakk> \<Longrightarrow> {inv_begin1 x} tcopy {inv_end0 x}"
   921 proof(simp add: tcopy_def, rule_tac Hoare_plus_halt)
   921 proof(simp add: tcopy_def, rule_tac Hoare_plus_halt)
   922   show "inv_loop0 x \<mapsto> inv_end1 x"
   922   show "inv_loop0 x \<mapsto> inv_end1 x"
   923     by(auto simp: inv_end1.simps inv_loop1.simps assert_imp_def)
   923     by(auto simp: inv_end1.simps inv_loop1.simps assert_imp_def)
   924 next
   924 next
   925   show "tm_wf (tcopy_init |+| tcopy_loop, 0)" by auto
   925   show "tm_wf (tcopy_begin |+| tcopy_loop, 0)" by auto
   926 next
   926 next
   927   assume "0 < x"
   927   assume "0 < x"
   928   thus "{inv_init1 x} (tcopy_init |+| tcopy_loop) {inv_loop0 x}"
   928   thus "{inv_begin1 x} (tcopy_begin |+| tcopy_loop) {inv_loop0 x}"
   929   proof(rule_tac Hoare_plus_halt)
   929   proof(rule_tac Hoare_plus_halt)
   930     show "inv_init0 x \<mapsto> inv_loop1 x"
   930     show "inv_begin0 x \<mapsto> inv_loop1 x"
   931       apply(auto simp: inv_init0.simps inv_loop1.simps assert_imp_def)
   931       apply(auto simp: inv_begin0.simps inv_loop1.simps assert_imp_def)
   932       apply(rule_tac x = "Suc 0" in exI, auto)
   932       apply(rule_tac x = "Suc 0" in exI, auto)
   933       done
   933       done
   934   next
   934   next
   935     show "tm_wf (tcopy_init, 0)" by auto
   935     show "tm_wf (tcopy_begin, 0)" by auto
   936   next
   936   next
   937     assume "0 < x"
   937     assume "0 < x"
   938     thus "{inv_init1 x} tcopy_init {inv_init0 x}"
   938     thus "{inv_begin1 x} tcopy_begin {inv_begin0 x}"
   939       by(erule_tac init_correct)
   939       by(erule_tac init_correct)
   940   next
   940   next
   941     assume "0 < x"
   941     assume "0 < x"
   942     thus "{inv_loop1 x} tcopy_loop {inv_loop0 x}"
   942     thus "{inv_loop1 x} tcopy_loop {inv_loop0 x}"
   943       by(erule_tac loop_correct)
   943       by(erule_tac loop_correct)
   947   thus "{inv_end1 x} tcopy_end {inv_end0 x}"
   947   thus "{inv_end1 x} tcopy_end {inv_end0 x}"
   948     by(erule_tac end_correct)
   948     by(erule_tac end_correct)
   949 qed
   949 qed
   950 
   950 
   951 abbreviation
   951 abbreviation
   952   "pre_tcopy n \<equiv> \<lambda>(l::cell list, r::cell list). ((l, r) = ([], <[n::nat]>))"
   952   "pre_tcopy n \<equiv> \<lambda>tp. tp = ([]::cell list, <[n::nat]>)"
   953 abbreviation
   953 abbreviation
   954   "post_tcopy n \<equiv> \<lambda>(l::cell list, r::cell list). ((l, r) = ([Bk], <[n, n::nat]>))"
   954   "post_tcopy n \<equiv> \<lambda>tp. tp= ([Bk], <[n, n::nat]>)"
   955 
   955 
   956 lemma tcopy_correct2:
   956 lemma tcopy_correct2:
   957   shows "{pre_tcopy n} tcopy {post_tcopy n}"
   957   shows "{pre_tcopy n} tcopy {post_tcopy n}"
   958 proof -
   958 proof -
   959   have "{inv_init1 (Suc n)} tcopy {inv_end0 (Suc n)}"
   959   have "{inv_begin1 (Suc n)} tcopy {inv_end0 (Suc n)}"
   960     by (rule tcopy_correct1) (simp)
   960     by (rule tcopy_correct1) (simp)
   961   moreover
   961   moreover
   962   have "pre_tcopy n \<mapsto> inv_init1 (Suc n)" 
   962   have "pre_tcopy n \<mapsto> inv_begin1 (Suc n)" 
   963     by (simp add: assert_imp_def tape_of_nl_abv)
   963     by (simp add: assert_imp_def tape_of_nl_abv)
   964   moreover
   964   moreover
   965   have "inv_end0 (Suc n) \<mapsto> post_tcopy n" 
   965   have "inv_end0 (Suc n) \<mapsto> post_tcopy n" 
   966     by (simp add: assert_imp_def inv_end0.simps tape_of_nl_abv)
   966     by (simp add: assert_imp_def inv_end0.simps tape_of_nl_abv)
   967   ultimately
   967   ultimately
   981   where
   981   where
   982   "dither \<equiv> [(W0, 1), (R, 2), (L, 1), (L, 0)] "
   982   "dither \<equiv> [(W0, 1), (R, 2), (L, 1), (L, 0)] "
   983 
   983 
   984 (* invariants of dither *)
   984 (* invariants of dither *)
   985 abbreviation
   985 abbreviation
   986   "dither_halt_inv \<equiv> \<lambda>(l, r). (\<exists>n. (l, r) = (Bk \<up> n, [Oc, Oc]))"
   986   "dither_halt_inv \<equiv> \<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <1::nat>))"
   987 
   987 
   988 abbreviation
   988 abbreviation
   989   "dither_unhalt_inv \<equiv> \<lambda>(l, r). (\<exists>n. (l, r) = (Bk \<up> n, [Oc]))"
   989   "dither_unhalt_inv \<equiv> \<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <0::nat>))"
   990 
   990 
   991 lemma dither_loops_aux: 
   991 lemma dither_loops_aux: 
   992   "(steps0 (1, Bk \<up> m, [Oc]) dither stp = (1, Bk \<up> m, [Oc])) \<or> 
   992   "(steps0 (1, Bk \<up> m, [Oc]) dither stp = (1, Bk \<up> m, [Oc])) \<or> 
   993    (steps0 (1, Bk \<up> m, [Oc]) dither stp = (2, Oc # Bk \<up> m, []))"
   993    (steps0 (1, Bk \<up> m, [Oc]) dither stp = (2, Oc # Bk \<up> m, []))"
   994   apply(induct stp)
   994   apply(induct stp)
   995   apply(auto simp: steps.simps step.simps dither_def numeral)
   995   apply(auto simp: steps.simps step.simps dither_def numeral tape_of_nat_abv)
   996   done
   996   done
   997 
   997 
   998 lemma dither_loops:
   998 lemma dither_loops:
   999   shows "{dither_unhalt_inv} dither \<up>" 
   999   shows "{dither_unhalt_inv} dither \<up>" 
  1000 apply(rule Hoare_unhaltI)
  1000 apply(rule Hoare_unhaltI)
  1001 using dither_loops_aux
  1001 using dither_loops_aux
  1002 apply(auto simp add: numeral)
  1002 apply(auto simp add: numeral tape_of_nat_abv)
  1003 by (metis Suc_neq_Zero is_final_eq)
  1003 by (metis Suc_neq_Zero is_final_eq)
  1004 
  1004 
  1005 
  1005 
  1006 lemma dither_halts_aux: 
  1006 lemma dither_halts_aux: 
  1007   shows "steps0 (1, Bk \<up> m, [Oc, Oc]) dither 3 = (0, Bk \<up> m, [Oc, Oc])"
  1007   shows "steps0 (1, Bk \<up> m, [Oc, Oc]) dither 3 = (0, Bk \<up> m, [Oc, Oc])"
  1010 
  1010 
  1011 lemma dither_halts:
  1011 lemma dither_halts:
  1012   shows "{dither_halt_inv} dither {dither_halt_inv}" 
  1012   shows "{dither_halt_inv} dither {dither_halt_inv}" 
  1013 apply(rule Hoare_haltI)
  1013 apply(rule Hoare_haltI)
  1014 using dither_halts_aux
  1014 using dither_halts_aux
  1015 apply(auto)
  1015 apply(auto simp add: tape_of_nat_abv)
  1016 by (metis (lifting, mono_tags) holds_for.simps is_final_eq prod.cases)
  1016 by (metis (lifting, mono_tags) holds_for.simps is_final_eq prod.cases)
  1017 
  1017 
  1018 
  1018 
  1019 
  1019 
  1020 section {* The diagnal argument below shows the undecidability of Halting problem *}
  1020 section {* The diagnal argument below shows the undecidability of Halting problem *}
  1022 text {*
  1022 text {*
  1023   @{text "haltP tp x"} means TM @{text "tp"} terminates on input @{text "x"}
  1023   @{text "haltP tp x"} means TM @{text "tp"} terminates on input @{text "x"}
  1024   and the final configuration is standard.
  1024   and the final configuration is standard.
  1025 *}
  1025 *}
  1026 
  1026 
  1027 definition haltP :: "tprog \<Rightarrow> nat list \<Rightarrow> bool"
  1027 definition haltP :: "tprog0 \<Rightarrow> nat list \<Rightarrow> bool"
  1028   where
  1028   where
  1029   "haltP p lm \<equiv> \<exists>n a m. steps (1, [], <lm>) p n = (0, Bk \<up> a,  <m::nat>)"
  1029   "haltP p lm \<equiv> {(\<lambda>tp. tp = ([], <lm>))} p {(\<lambda>tp. (\<exists>k m. tp = (Bk \<up> k,  <m::nat>)))}"
  1030 
       
  1031 abbreviation
       
  1032   "haltP0 p lm \<equiv> haltP (p, 0) lm" 
       
  1033 
  1030 
  1034 lemma [intro, simp]: "tm_wf0 tcopy"
  1031 lemma [intro, simp]: "tm_wf0 tcopy"
  1035 by (auto simp: tcopy_def)
  1032 by (auto simp: tcopy_def)
  1036 
  1033 
  1037 lemma [intro, simp]: "tm_wf0 dither"
  1034 lemma [intro, simp]: "tm_wf0 dither"
  1055   assumes h_wf[intro]: "tm_wf0 H"
  1052   assumes h_wf[intro]: "tm_wf0 H"
  1056   -- {*
  1053   -- {*
  1057   The following two assumptions specifies that @{text "H"} does solve the Halting problem.
  1054   The following two assumptions specifies that @{text "H"} does solve the Halting problem.
  1058   *}
  1055   *}
  1059   and h_case: 
  1056   and h_case: 
  1060   "\<And> M lm. (haltP0 M lm) \<Longrightarrow> \<exists>stp n. (steps0 (1, [Bk], <code M#lm>) H stp = (0, Bk \<up> n, [Oc]))"
  1057   "\<And> M lm. haltP M lm \<Longrightarrow> {(\<lambda>tp. tp = ([Bk], <code M#lm>))} H {(\<lambda>tp. \<exists>n. tp = (Bk \<up> n, <0::nat>))}"
  1061   and nh_case: 
  1058   and nh_case: 
  1062   "\<And> M lm. (\<not> haltP0 M lm) \<Longrightarrow> \<exists>stp n. (steps0 (1, [Bk], <code M#lm>) H stp = (0, Bk \<up> n, [Oc, Oc]))"
  1059   "\<And> M lm. \<not> haltP M lm \<Longrightarrow> {(\<lambda>tp. tp = ([Bk], <code M#lm>))} H {(\<lambda>tp. \<exists>n. tp = (Bk \<up> n, <1::nat>))}"
  1063 begin
  1060 begin
  1064 
  1061 
  1065 (* invariants for H *)
  1062 (* invariants for H *)
  1066 abbreviation
  1063 abbreviation
  1067   "pre_H_inv M n \<equiv> \<lambda>(l::cell list, r::cell list). ((l, r) = ([Bk], <[code M, n]>))"
  1064   "pre_H_inv M n \<equiv> \<lambda>tp. tp = ([Bk], <[code M, n]>)"
  1068 
  1065 
  1069 abbreviation
  1066 abbreviation
  1070   "post_H_halt_inv \<equiv> \<lambda>(l, r). (\<exists>n. (l, r) = (Bk \<up> n, [Oc, Oc]))"
  1067   "post_H_halt_inv \<equiv> \<lambda>tp. \<exists>n. tp = (Bk \<up> n, <1::nat>)"
  1071 
  1068 
  1072 abbreviation
  1069 abbreviation
  1073   "post_H_unhalt_inv \<equiv> \<lambda>(l, r). (\<exists>n. (l, r) = (Bk \<up> n, [Oc]))"
  1070   "post_H_unhalt_inv \<equiv> \<lambda>tp. \<exists>n. tp = (Bk \<up> n, <0::nat>)"
  1074 
  1071 
  1075 
  1072 
  1076 lemma H_halt_inv:
  1073 lemma H_halt_inv:
  1077   assumes "\<not> haltP0 M [n]" 
  1074   assumes "\<not> haltP M [n]" 
  1078   shows "{pre_H_inv M n} H {post_H_halt_inv}"
  1075   shows "{pre_H_inv M n} H {post_H_halt_inv}"
  1079 proof -
  1076 using assms nh_case by auto
  1080   obtain stp i
       
  1081     where "steps0 (1, [Bk], <[code M, n]>) H stp = (0, Bk \<up> i, [Oc, Oc])"
       
  1082     using nh_case assms by blast
       
  1083   then show "{pre_H_inv M n} H {post_H_halt_inv}"
       
  1084     unfolding Hoare_halt_def
       
  1085     apply(auto)
       
  1086     apply(rule_tac x = stp in exI)
       
  1087     apply(auto)
       
  1088     done
       
  1089 qed
       
  1090 
  1077 
  1091 lemma H_unhalt_inv:
  1078 lemma H_unhalt_inv:
  1092   assumes "haltP0 M [n]" 
  1079   assumes "haltP M [n]" 
  1093   shows "{pre_H_inv M n} H {post_H_unhalt_inv}"
  1080   shows "{pre_H_inv M n} H {post_H_unhalt_inv}"
  1094 proof -
  1081 using assms h_case by auto
  1095   obtain stp i
       
  1096     where "steps0 (1, [Bk], <[code M, n]>) H stp = (0, Bk \<up> i, [Oc])"
       
  1097     using h_case assms by blast
       
  1098   then show "{pre_H_inv M n} H {post_H_unhalt_inv}"
       
  1099     unfolding Hoare_halt_def
       
  1100     apply(auto)
       
  1101     apply(rule_tac x = stp in exI)
       
  1102     apply(auto)
       
  1103     done
       
  1104 qed
       
  1105 
       
  1106    
  1082    
  1107 (* TM that produces the contradiction and its code *)
  1083 (* TM that produces the contradiction and its code *)
  1108 abbreviation 
  1084 abbreviation 
  1109   "tcontra \<equiv> (tcopy |+| H) |+| dither"
  1085   "tcontra \<equiv> (tcopy |+| H) |+| dither"
  1110 abbreviation
  1086 abbreviation
  1111   "code_tcontra \<equiv> code tcontra"
  1087   "code_tcontra \<equiv> code tcontra"
  1112 
  1088 
  1113 (* assume tcontra does not halt on its code *)
  1089 (* assume tcontra does not halt on its code *)
  1114 lemma tcontra_unhalt: 
  1090 lemma tcontra_unhalt: 
  1115   assumes "\<not> haltP0 tcontra [code tcontra]"
  1091   assumes "\<not> haltP tcontra [code tcontra]"
  1116   shows "False"
  1092   shows "False"
  1117 proof -
  1093 proof -
  1118   (* invariants *)
  1094   (* invariants *)
  1119   def P1 \<equiv> "\<lambda>(l::cell list, r::cell list). (l, r) = ([], <[code_tcontra]>)"
  1095   def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <[code_tcontra]>)"
  1120   def P2 \<equiv> "\<lambda>(l::cell list, r::cell list). (l, r) = ([Bk], <[code_tcontra, code_tcontra]>)"
  1096   def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <[code_tcontra, code_tcontra]>)"
  1121   def P3 \<equiv> "\<lambda>(l, r). (\<exists>n. (l, r) = (Bk \<up> n, [Oc, Oc]))"
  1097   def P3 \<equiv> "\<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <1::nat>))"
  1122 
  1098 
  1123   (*
  1099   (*
  1124   {P1} tcopy {P2}  {P2} H {P3} 
  1100   {P1} tcopy {P2}  {P2} H {P3} 
  1125   ----------------------------
  1101   ----------------------------
  1126      {P1} (tcopy |+| H) {P3}     {P3} dither {P3}
  1102      {P1} (tcopy |+| H) {P3}     {P3} dither {P3}
  1138       by (rule tcopy_correct2)
  1114       by (rule tcopy_correct2)
  1139   next
  1115   next
  1140     case B_halt (* of H *)
  1116     case B_halt (* of H *)
  1141     show "{P2} H {P3}"
  1117     show "{P2} H {P3}"
  1142       unfolding P2_def P3_def
  1118       unfolding P2_def P3_def
  1143       using assms by (rule H_halt_inv)
  1119       by (rule H_halt_inv[OF assms])
  1144   qed (simp)
  1120   qed (simp)
  1145 
  1121 
  1146   (* {P3} dither {P3} *)
  1122   (* {P3} dither {P3} *)
  1147   have second: "{P3} dither {P3}" unfolding P3_def 
  1123   have second: "{P3} dither {P3}" unfolding P3_def 
  1148     by (rule dither_halts)
  1124     by (rule dither_halts)
  1157     unfolding Hoare_halt_def 
  1133     unfolding Hoare_halt_def 
  1158     apply(auto)
  1134     apply(auto)
  1159     apply(drule_tac x = n in spec)
  1135     apply(drule_tac x = n in spec)
  1160     apply(case_tac "steps0 (Suc 0, [], <[code tcontra]>) tcontra n")
  1136     apply(case_tac "steps0 (Suc 0, [], <[code tcontra]>) tcontra n")
  1161     apply(auto)
  1137     apply(auto)
  1162     apply(drule_tac x = 1 in spec)
       
  1163     apply(simp add: numeral tape_of_nat_abv)
       
  1164     done
  1138     done
  1165 qed
  1139 qed
  1166 
  1140 
  1167 (* asumme tcontra halts on its code *)
  1141 (* asumme tcontra halts on its code *)
  1168 lemma tcontra_halt: 
  1142 lemma tcontra_halt: 
  1169   assumes "haltP0 tcontra [code tcontra]"
  1143   assumes "haltP tcontra [code tcontra]"
  1170   shows "False"
  1144   shows "False"
  1171 proof - 
  1145 proof - 
  1172   (* invariants *)
  1146   (* invariants *)
  1173   def P1 \<equiv> "\<lambda>(l::cell list, r::cell list). (l, r) = ([], <[code_tcontra]>)"
  1147   def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <[code_tcontra]>)"
  1174   def P2 \<equiv> "\<lambda>(l::cell list, r::cell list). (l, r) = ([Bk], <[code_tcontra, code_tcontra]>)"
  1148   def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <[code_tcontra, code_tcontra]>)"
  1175   def P3 \<equiv> "\<lambda>(l::cell list, r::cell list). (\<exists>n. (l, r) = (Bk \<up> n, [Oc]))"
  1149   def P3 \<equiv> "\<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <0::nat>))"
  1176 
  1150 
  1177   (*
  1151   (*
  1178   {P1} tcopy {P2}  {P2} H {P3} 
  1152   {P1} tcopy {P2}  {P2} H {P3} 
  1179   ----------------------------
  1153   ----------------------------
  1180      {P1} (tcopy |+| H) {P3}     {P3} dither loops
  1154      {P1} (tcopy |+| H) {P3}     {P3} dither loops
  1192       by (rule tcopy_correct2)
  1166       by (rule tcopy_correct2)
  1193   next
  1167   next
  1194     case B_halt (* of H *)
  1168     case B_halt (* of H *)
  1195     then show "{P2} H {P3}"
  1169     then show "{P2} H {P3}"
  1196       unfolding P2_def P3_def
  1170       unfolding P2_def P3_def
  1197       using assms by (rule H_unhalt_inv)
  1171       by (rule H_unhalt_inv[OF assms])
  1198   qed (simp)
  1172   qed (simp)
  1199 
  1173 
  1200   (* {P3} dither loops *)
  1174   (* {P3} dither loops *)
  1201   have second: "{P3} dither \<up>" unfolding P3_def 
  1175   have second: "{P3} dither \<up>" unfolding P3_def 
  1202     by (rule dither_loops)
  1176     by (rule dither_loops)
  1206     by (rule Hoare_plus_unhalt_simple[OF first second H_wf])
  1180     by (rule Hoare_plus_unhalt_simple[OF first second H_wf])
  1207 
  1181 
  1208   with assms show "False"
  1182   with assms show "False"
  1209     unfolding P1_def
  1183     unfolding P1_def
  1210     unfolding haltP_def
  1184     unfolding haltP_def
  1211     unfolding Hoare_unhalt_def
  1185     unfolding Hoare_halt_def Hoare_unhalt_def
  1212     by (auto, metis is_final_eq)  
  1186     by auto
  1213 qed
  1187 qed
  1214       
  1188       
  1215 text {*
  1189 text {*
  1216   @{text "False"} can finally derived.
  1190   @{text "False"} can finally derived.
  1217 *}
  1191 *}