\documentclass{article}

\title{The halting problem illustrated with C functions}
\author{Fr\'ed\'eric Lang}
\date{\today}

\usepackage{listings}

\lstdefinelanguage{SmallC}
{morekeywords={
     boolean, void, return, for, if, else
},
sensitive=true,
morecomment=[s]{/*}{*/},
morestring=[b]"
}

\lstset{language=SmallC,
    basicstyle=\small\sffamily
}

\begin{document}

\maketitle

In this problem, assume that there exists a C function \textsf{terminate()}
with the following interface:

\begin{lstlisting}
boolean terminate (void *p);
/* 
 * Precondition: p is a pointer to a parameter-free function
 * Postcondition: return true if and only if (*p)() terminates
 * Property: terminate (*p) terminates on any input p
 */
\end{lstlisting}

We are going to show that such a terminating {\sf terminate()} function cannot
exist\footnote{Note that if such a function cannot exist for parameter-free
functions, then it cannot exist at all for more general functions, since
general functions include parameter-free functions.}.
To this aim, we define the following four functions \textsf{skip()},
\textsf{loop()}, \textsf{loop\_if\_terminate()}, and
\textsf{self\_loop\_if\_terminate()}:

\begin{lstlisting}
void skip () {}   /* do nothing and terminate */

void loop () {
     for ( ; ; ); /* do nothing and never terminate */
}

boolean loop_if_terminate (void *p) {
     boolean b = terminate (p);
     if (b) loop (); else return b;
}

boolean self_loop_if_terminate () {
     return loop_if_terminate (&self_loop_if_terminate);
}
\end{lstlisting}

\noindent\textbf{Questions:}

\begin{enumerate} 

\item Consider the parameter-free functions {\sf f01()} to {\sf f12()},
defined in Figure~\ref{fig:functions}.
How should calls to these functions behave? Indicate whether the function
should loop indefinitely or terminate, and in the latter case, the result it
should return.
You do not need to justify your answer.

\begin{figure}
\begin{lstlisting}
boolean f01 () { return terminate (&loop); }
boolean f02 () { return terminate (&skip); }
boolean f03 () { return loop_if_terminate (&loop); }
boolean f04 () { return loop_if_terminate (&skip); }
boolean f05 () { return terminate (&f01); }
boolean f06 () { return terminate (&f02); }
boolean f07 () { return terminate (&f03); }
boolean f08 () { return terminate (&f04); }
boolean f09 () { return loop_if_terminate (&f01); }
boolean f10 () { return loop_if_terminate (&f02); }
boolean f11 () { return loop_if_terminate (&f03); }
boolean f12 () { return loop_if_terminate (&f04); }
\end{lstlisting}
\caption{Various functions}
\label{fig:functions}
\end{figure}

\item Assume that the call \textsf{self\_loop\_if\_terminate ()} does not
terminate.  How can you contradict that asumption?

\item Assume that the call \textsf{self\_loop\_if\_terminate ()} does
terminate.  How can you contradict that asumption again?

\item\label{q1} What can you conclude about the existence of a function
having the behaviour intended for \textsf{terminate()}?

\item\label{q2} Is C Turing complete?  How would you prove this fact?

\item\label{q3} In theory, can any C program be emulated by a Turing Machine?

\item How do you come from your conclusions at
questions~\ref{q1},~\ref{q2}, and~\ref{q3}, to a famous result in
theoretical computer science? 

\end{enumerate}

\end{document}
