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