81530
|
1 |
(*
|
|
2 |
Undecidability of the special Halting problem:
|
|
3 |
Does a program applied to an encoding of itself terminate?
|
|
4 |
Author: Fabian Huch
|
|
5 |
*)
|
|
6 |
|
|
7 |
theory Halting
|
|
8 |
imports "HOL-IMP.Big_Step"
|
|
9 |
begin
|
|
10 |
|
|
11 |
definition "halts c s \<equiv> (\<exists>s'. (c, s) \<Rightarrow> s')"
|
|
12 |
|
|
13 |
text \<open>A simple program that does not halt:\<close>
|
|
14 |
definition "loop \<equiv> WHILE Bc True DO SKIP"
|
|
15 |
|
|
16 |
lemma loop_never_halts[simp]: "\<not> halts loop s"
|
|
17 |
unfolding halts_def
|
|
18 |
proof clarify
|
|
19 |
fix s' assume "(loop, s) \<Rightarrow> s'"
|
|
20 |
then show False
|
|
21 |
by (induction loop s s' rule: big_step_induct) (simp_all add: loop_def)
|
|
22 |
qed
|
|
23 |
|
|
24 |
section \<open>Halting Problem\<close>
|
|
25 |
|
|
26 |
text \<open>
|
|
27 |
Given any encoding \<open>f\<close> (of programs to states), there is no Program \<open>H\<close> such that
|
|
28 |
for all programs \<open>c\<close>, \<open>H\<close> terminates in a state \<open>s'\<close> which has at variable \<open>x\<close> the
|
|
29 |
answer whether \<open>c\<close> halts.\<close>
|
|
30 |
|
|
31 |
theorem halting:
|
|
32 |
"\<nexists>H. \<forall>c. \<exists>s'. (H, f c) \<Rightarrow> s' \<and> (halts c (f c) \<longleftrightarrow> s' x > 0)"
|
|
33 |
(is "\<nexists>H. ?P H")
|
|
34 |
proof clarify
|
|
35 |
fix H assume assm: "?P H"
|
|
36 |
|
|
37 |
\<comment> \<open>inverted H: loops if input halts\<close>
|
|
38 |
let ?inv_H = "H ;; IF Less (V x) (N 1) THEN SKIP ELSE loop"
|
|
39 |
|
|
40 |
\<comment> \<open>compute in \<open>s'\<close> whether inverted \<open>H\<close> halts when applied to itself\<close>
|
|
41 |
obtain s' where
|
|
42 |
s'_def: "(H, f ?inv_H) \<Rightarrow> s'" and
|
|
43 |
s'_halts: "halts ?inv_H (f ?inv_H) \<longleftrightarrow> (s' x > 0)"
|
|
44 |
using assm by blast
|
|
45 |
|
|
46 |
\<comment> \<open>contradiction: if it terminates, loop must have terminated; if not, SKIP must have looped!\<close>
|
|
47 |
show False
|
|
48 |
proof(cases "halts ?inv_H (f ?inv_H)")
|
|
49 |
case True
|
|
50 |
|
|
51 |
then have "halts (IF Less (V x) (N 1) THEN SKIP ELSE loop) s'"
|
|
52 |
unfolding halts_def using big_step_determ s'_def by fast
|
|
53 |
|
|
54 |
then have "halts loop s'"
|
|
55 |
using s'_halts True halts_def by auto
|
|
56 |
|
|
57 |
then show False by auto
|
|
58 |
next
|
|
59 |
case False
|
|
60 |
|
|
61 |
then have "\<not> halts SKIP s'"
|
|
62 |
using s'_def s'_halts halts_def by force
|
|
63 |
|
|
64 |
then show False using halts_def by auto
|
|
65 |
qed
|
|
66 |
qed
|
|
67 |
|
|
68 |
end
|