# HG changeset patch # User nipkow # Date 1733424293 -3600 # Node ID 41b387d477398579652844c92f1a98982e500ea6 # Parent 92a3017f7d1a499666acfbd1d59d9b4b5bc33051 added Halting problem theory diff -r 92a3017f7d1a -r 41b387d47739 src/HOL/IMP/Halting.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/IMP/Halting.thy Thu Dec 05 19:44:53 2024 +0100 @@ -0,0 +1,68 @@ +(* +Undecidability of the special Halting problem: + Does a program applied to an encoding of itself terminate? +Author: Fabian Huch +*) + +theory Halting + imports "HOL-IMP.Big_Step" +begin + +definition "halts c s \ (\s'. (c, s) \ s')" + +text \A simple program that does not halt:\ +definition "loop \ WHILE Bc True DO SKIP" + +lemma loop_never_halts[simp]: "\ halts loop s" + unfolding halts_def +proof clarify + fix s' assume "(loop, s) \ s'" + then show False + by (induction loop s s' rule: big_step_induct) (simp_all add: loop_def) +qed + +section \Halting Problem\ + +text \ +Given any encoding \f\ (of programs to states), there is no Program \H\ such that +for all programs \c\, \H\ terminates in a state \s'\ which has at variable \x\ the +answer whether \c\ halts.\ + +theorem halting: + "\H. \c. \s'. (H, f c) \ s' \ (halts c (f c) \ s' x > 0)" + (is "\H. ?P H") +proof clarify + fix H assume assm: "?P H" + + \ \inverted H: loops if input halts\ + let ?inv_H = "H ;; IF Less (V x) (N 1) THEN SKIP ELSE loop" + + \ \compute in \s'\ whether inverted \H\ halts when applied to itself\ + obtain s' where + s'_def: "(H, f ?inv_H) \ s'" and + s'_halts: "halts ?inv_H (f ?inv_H) \ (s' x > 0)" + using assm by blast + + \ \contradiction: if it terminates, loop must have terminated; if not, SKIP must have looped!\ + show False + proof(cases "halts ?inv_H (f ?inv_H)") + case True + + then have "halts (IF Less (V x) (N 1) THEN SKIP ELSE loop) s'" + unfolding halts_def using big_step_determ s'_def by fast + + then have "halts loop s'" + using s'_halts True halts_def by auto + + then show False by auto + next + case False + + then have "\ halts SKIP s'" + using s'_def s'_halts halts_def by force + + then show False using halts_def by auto + qed +qed + +end diff -r 92a3017f7d1a -r 41b387d47739 src/HOL/ROOT --- a/src/HOL/ROOT Thu Dec 05 15:49:48 2024 +0100 +++ b/src/HOL/ROOT Thu Dec 05 19:44:53 2024 +0100 @@ -268,6 +268,7 @@ Procs_Stat_Vars_Stat C_like OO + Halting document_files "root.bib" "root.tex" session "HOL-IMPP" in IMPP = HOL +