The Tiling Problem The Halting Problem Highest precedence Java s Precedence G!del, Escher, Bach Natural, yet unsolvable problems Adding variables Adding operators Lecture 19 Lab 4: A Matter of Expression BNF More on Boolean Expressions Unary operators: ++, --,!, unary - and +, type-cast Multiplication and division: *, /, % Addition and subtraction: +, - Relational operators: <, >, <=, >= Equality and inequality: ==,!= Boolean and: && Boolean or: Conditional operator:?: Assignment operators: =, +=, -=, *=, /=, %= Lowest precedence 2 Parse tree Extended Grammar for Boolean Expressions: Implication <bexpr> ::= <bterm> <bexpr > <bexpr > ::= <bterm> <bexpr > -> <bterm> <bexpr > e <bterm> ::= <bfactor> <bterm > <bterm > ::= && <bfactor> <bterm > e <bfactor> ::=! <bfactor> ( <bexpr> ) <boolconst> variable 3 <boolconst> ::= true false 4
Truth table P Q P! Q P " Q P # Q Imp 5 6 Two operators sharing the same symbol <bexpr> ::= <bterm> <bexpr > <bexpr > ::= <bterm> <bexpr > # <bterm> <bexpr > e <bterm> ::= <bfactor> <bterm > <bterm > ::= && <bfactor> <bterm > e <bfactor> ::=! <bfactor> # <bfactor> ( <bexpr> ) <boolconst> variable <boolconst> ::= true false 7 BNF expressed in BNF <syntax> ::= { <rule> } <rule> ::= <id> ::= <bnfexpr> <bnfexpr> ::= <bnfterm> { <bnfterm> } <bnfterm> ::= <bnffactor> { <bnffactor> } <bnffactor> ::= <id> <quoted_symb> ( <bnfexpr> ) [ <bnfexpr> ] { <bnfexpr> } <id> ::= letter { letter digit } <quoted_symb> ::= { any_character } 8
Computational aspects on problems Many problems are solvable in a finite number of steps. Easy problems with polynomial-time algorithms. Harder problems with exponential time algorithms. Some of these could be easier but no one knows But are all (computational) problems solvable in finite time or even in infinite time.. or at all??! Unfortunately, No. 9 Unsolvable problems (Last lecture: The Tiling Problem: Given a set of k different quadratic 1!1 tiles, can they (any number of each kind) be used to cover all rectangular regions?) Today: The Halting Problem: Given a computer program as input, will that program terminate when given some input I? 10 Preliminaries We only consider programs that get their input when it is requested. A user cannot stall the program indefinitely by refusing to enter data via, say, the keyboard. Computer programs can be represented as strings. This is also true for the input. Source code. A program either terminates or runs forever. To save space we use methods instead of programs. The argument is based on proof by contradiction : Assume X. If we can then establish a contradiction after having argued (correctly), we have proved and can conclude not X. A formal proof would be much more complicated then what is shown here. 11 The Halting Problem Assume that there is a method (a computer program) halt(string s, String i) that given a program s and an input i determines whether s terminates or not when given i as input. The result is either yes or no. Consider this method (computer program): public static String trouble(string x) { if (halt(x, x)== no ) { return yes ; } else { while (true) {}; // infinite loop } } 12
The Halting Problem Let t be the string that represents the method trouble. Then, what is the result (output) of program halt(t, t)? input The Halting Problem Alan Turing, On computable numbers, with an application to the Entscheidungsproblem, Proceedings of the London Mathematical Society, Series 2, 42 (1936), pp 230-265. If no, trouble doesn t terminate if given t as input. But (looking at the code of trouble) that can only happen if trouble terminates! A contradiction. If yes, trouble terminates. But then (again by looking at the code of trouble) if it really terminates the result of halt(t,t) must be no! Yet a contradiction. We conclude that halt doesn t exist. So, the halting problem has no solution; there is no algorithm that solves the problem(!) 13 Alan Turing 14 Comment As stated, the Halting Problem must work for all possible programs. In our proof, we used one program (trouble) to reach contradictions. However, there might be special kinds of programs that can be checked. A common and practical approach to hard problems is to only consider restricted (easy) cases that can be solved efficiently. Gödel, Escher, Bach - An Eternal Golden Braid Douglas R. Hofstadtler (ISBN 0465026567). Pulitzer Prize winner. A book about how the creative achievements of logician Kurt Gödel, artist M. C. Escher and composer Johan Sebastian Bach interweave. As the author states: "I realized that to me, Gödel and Escher and Bach were only shadows cast in different directions by some central solid essence. I tried to reconstruct the central object, and came up with this book. 15 16
"Do words and thoughts follow formal rules, or do they not?" "GEB is a very personal attempt to say how it is that animate beings can come out of inanimate matter. What is a self, and how can a self come out of stuff that is as selfless as a stone or a puddle?" 17 18 20 M. C. Escher (1898-1972) Ascending and Descending, 1960. Drawing Hands, 1948 Johann Sebastian Bach (1685-1750) Two-part invention No. 8, F-Major, BWV 779 (1723) 19
K. Gödel, Über formal unentscheidbare Sätze der Principia Mathematica und verwandter Systeme. I. Monatsh. Math. Phys. 38, 173-198 (1931). René Magritte (1898-1967) Belgian painter Surrealist The Incompleteness Theorem (In any consistent formalization of mathematics that is sufficiently strong to axiomatize the natural numbers, that is, sufficiently strong to define the operations that collectively define the natural numbers ) One can construct a true (!) statement that can be neither proved nor disproved within that system itself. Kurt Gödel (1906-1978) 21 23 22