Paper/Paper.thy
changeset 104 01f688735b9b
parent 103 294576baaeed
child 105 2cae8a39803e
equal deleted inserted replaced
103:294576baaeed 104:01f688735b9b
   233 \emph{not} formalise the undecidability of the halting problem since
   233 \emph{not} formalise the undecidability of the halting problem since
   234 their main focus is complexity, rather than computability theory. They
   234 their main focus is complexity, rather than computability theory. They
   235 also report that the informal proofs from which they started are not
   235 also report that the informal proofs from which they started are not
   236 ``sufficiently accurate to be directly usable as a guideline for
   236 ``sufficiently accurate to be directly usable as a guideline for
   237 formalization'' \cite[Page 2]{AspertiRicciotti12}. For our
   237 formalization'' \cite[Page 2]{AspertiRicciotti12}. For our
   238 formalisation we follow mainly the proofs from the textbook
   238 formalisation we follow mainly the proofs from the textbook by Boolos
   239 \cite{Boolos87} and found that the description there is quite
   239 et al \cite{Boolos87} and found that the description there is quite
   240 detailed. Some details are left out however: for example, constructing
   240 detailed. Some details are left out however: for example, constructing
   241 the \emph{copy Turing machine} and its correctness proof are left as
   241 the \emph{copy Turing machine} is left as an excerise to the
   242 an excerise to the reader; also \cite{Boolos87} only shows how the
   242 reader---a correctness proof is not mentioned at all; also \cite{Boolos87}
   243 universal Turing machine is constructed for Turing machines computing
   243 only shows how the universal Turing machine is constructed for Turing
   244 unary functions. We had to figure out a way to generalise this result
   244 machines computing unary functions. We had to figure out a way to
   245 to $n$-ary functions. Similarly, when compiling recursive functions to
   245 generalise this result to $n$-ary functions. Similarly, when compiling
   246 abacus machines, the textbook again only shows how it can be done for
   246 recursive functions to abacus machines, the textbook again only shows
   247 2- and 3-ary functions, but in the formalisation we need arbitrary
   247 how it can be done for 2- and 3-ary functions, but in the
   248 functions. But the general ideas for how to do this are clear enough
   248 formalisation we need arbitrary functions. But the general ideas for
   249 in \cite{Boolos87}.
   249 how to do this are clear enough in \cite{Boolos87}.
   250 %However, one
   250 %However, one
   251 %aspect that is completely left out from the informal description in
   251 %aspect that is completely left out from the informal description in
   252 %\cite{Boolos87}, and similar ones we are aware of, is arguments why certain Turing
   252 %\cite{Boolos87}, and similar ones we are aware of, is arguments why certain Turing
   253 %machines are correct. We will introduce Hoare-style proof rules
   253 %machines are correct. We will introduce Hoare-style proof rules
   254 %which help us with such correctness arguments of Turing machines.
   254 %which help us with such correctness arguments of Turing machines.
   553   & & @{text "in (s', update (l, r) a)"}
   553   & & @{text "in (s', update (l, r) a)"}
   554   \end{tabular}
   554   \end{tabular}
   555   \end{center}
   555   \end{center}
   556 
   556 
   557   \noindent
   557   \noindent
   558   where @{term "read r"} returns the head of the list @{text r}, or if @{text r} is
   558   where @{term "read r"} returns the head of the list @{text r}, or if
   559   empty it returns @{term Bk}.
   559   @{text r} is empty it returns @{term Bk}.  It is impossible in
   560   It is impossible in Isabelle/HOL to lift the @{term step}-function to realise
   560   Isabelle/HOL to lift the @{term step}-function in order to realise a
   561   a general evaluation function for Turing machines. The reason is that functions in HOL-based
   561   general evaluation function for Turing machines programs. The reason
   562   provers need to be terminating, and clearly there are Turing machine 
   562   is that functions in HOL-based provers need to be terminating, and
   563   programs that are not. We can however define an evaluation
   563   clearly there are programs that are not. We can however define a
   564   function that performs exactly @{text n} steps:
   564   recursive evaluation function that performs exactly @{text n} steps:
   565 
   565 
   566   \begin{center}
   566   \begin{center}
   567   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   567   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   568   @{thm (lhs) steps.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(1)}\\
   568   @{thm (lhs) steps.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(1)}\\
   569   @{thm (lhs) steps.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(2)}\\
   569   @{thm (lhs) steps.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(2)}\\
   692   block of @{term "Oc"}s. The resulting tape is @{term "([Bk], <(2::nat, 2::nat)>)"}.}
   692   block of @{term "Oc"}s. The resulting tape is @{term "([Bk], <(2::nat, 2::nat)>)"}.}
   693   \label{copy}
   693   \label{copy}
   694   \end{figure}
   694   \end{figure}
   695 
   695 
   696 
   696 
   697   We often need to restrict tapes to be in \emph{standard form}, which means 
   697   We often need to restrict tapes to be in standard form, which means 
   698   the left list of the tape is either empty or only contains @{text "Bk"}s, and 
   698   the left list of the tape is either empty or only contains @{text "Bk"}s, and 
   699   the right list contains some ``clusters'' of @{text "Oc"}s separted by single 
   699   the right list contains some ``clusters'' of @{text "Oc"}s separted by single 
   700   blanks. To make this formal we define the following overloaded function
   700   blanks. To make this formal we define the following overloaded function
       
   701   encoding natural numbers into lists of @{term "Oc"}s and @{term Bk}s.
   701   
   702   
   702   \begin{center}
   703   \begin{center}
   703   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   704   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   704   @{thm (lhs) nats2tape(6)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(6)}\\
   705   @{thm (lhs) nats2tape(6)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(6)}\\
   705   @{thm (lhs) nats2tape(4)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(4)}\\
   706   @{thm (lhs) nats2tape(4)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(4)}\\
   710   @{thm (lhs) nats2tape(3)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(3)}
   711   @{thm (lhs) nats2tape(3)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(3)}
   711   \end{tabular}
   712   \end{tabular}
   712   \end{center}
   713   \end{center}
   713 
   714 
   714   \noindent
   715   \noindent
   715   A standard tape is then of the form @{text "(Bk\<^isup>l,\<langle>[n\<^isub>1,...,n\<^isub>m]\<rangle>)"} for some @{text l} 
   716   A \emph{standard tape} is then of the form @{text "(Bk\<^isup>l,\<langle>[n\<^isub>1,...,n\<^isub>m]\<rangle>)"} for some @{text l} 
   716   and @{text "n\<^isub>i"}. Note that the head in a standard tape ``points'' to the 
   717   and @{text "n\<^isub>i"}. Note that the head in a standard tape ``points'' to the 
   717   leftmost @{term "Oc"} on the tape. Note also that the natural number @{text 0} 
   718   leftmost @{term "Oc"} on the tape. Note also that the natural number @{text 0} 
   718   is represented by a single filled cell on a standard tape, @{text 1} by two filled cells and so on.
   719   is represented by a single filled cell on a standard tape, @{text 1} by two filled cells and so on.
   719 
   720 
   720   Before we can prove the undecidability of the halting problem for our
   721   Before we can prove the undecidability of the halting problem for
   721   Turing machines, we have to analyse two concrete Turing machine
   722   our Turing machines working on standard tapes, we have to analyse
   722   programs and establish that they are correct---that means they are
   723   two concrete Turing machine programs and establish that they are
   723   ``doing what they are supposed to be doing''.  Such correctness proofs are usually left
   724   correct---that means they are ``doing what they are supposed to be
   724   out in the informal literature, for example \cite{Boolos87}.  The first program 
   725   doing''.  Such correctness proofs are usually left out in the
   725   we need to prove correct is the @{term dither} program shown in \eqref{dither} 
   726   informal literature, for example \cite{Boolos87}.  The first program
   726   and the second program is @{term "tcopy"} defined as
   727   we need to prove correct is the @{term dither} program shown in
       
   728   \eqref{dither} and the second program is @{term "tcopy"} defined as
   727 
   729 
   728   \begin{equation}
   730   \begin{equation}
   729   \mbox{\begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   731   \mbox{\begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   730   @{thm (lhs) tcopy_def} & @{text "\<equiv>"} & @{thm (rhs) tcopy_def}
   732   @{thm (lhs) tcopy_def} & @{text "\<equiv>"} & @{thm (rhs) tcopy_def}
   731   \end{tabular}}\label{tcopy}
   733   \end{tabular}}\label{tcopy}
   737   defined in terms of \emph{Hoare-triples}, written @{term "{P} p
   739   defined in terms of \emph{Hoare-triples}, written @{term "{P} p
   738   {Q}"}.  They implement the idea that a program @{term
   740   {Q}"}.  They implement the idea that a program @{term
   739   p} started in state @{term "1::nat"} with a tape satisfying @{term
   741   p} started in state @{term "1::nat"} with a tape satisfying @{term
   740   P} will after some @{text n} steps halt (have transitioned into the
   742   P} will after some @{text n} steps halt (have transitioned into the
   741   halting state) with a tape satisfying @{term Q}. This idea is very
   743   halting state) with a tape satisfying @{term Q}. This idea is very
   742   similar to \emph{realisability} in \cite{AspertiRicciotti12}. We
   744   similar to the notion of \emph{realisability} in \cite{AspertiRicciotti12}. We
   743   also have \emph{Hoare-pairs} of the form @{term "{P} p \<up>"}
   745   also have \emph{Hoare-pairs} of the form @{term "{P} p \<up>"}
   744   implementing the case that a program @{term p} started with a tape
   746   implementing the case that a program @{term p} started with a tape
   745   satisfying @{term P} will loop (never transition into the halting
   747   satisfying @{term P} will loop (never transition into the halting
   746   state). Both notion are formally defined as
   748   state). Both notion are formally defined as
   747 
   749 
   769 
   771 
   770   \begin{equation}
   772   \begin{equation}
   771   @{thm[mode=Rule] Hoare_consequence}
   773   @{thm[mode=Rule] Hoare_consequence}
   772   \end{equation}
   774   \end{equation}
   773 
   775 
   774 
   776   \noindent
   775   \noindent
   777   where
       
   778   @{term "P' \<mapsto> P"} stands for the fact that for all tapes @{term "tp"},
       
   779   @{term "P' tp"} implies @{term "P tp"}.
       
   780 
   776   Like Asperti and Ricciotti with their notion of realisability, we
   781   Like Asperti and Ricciotti with their notion of realisability, we
   777   have set up our Hoare-rules so that we can deal explicitly
   782   have set up our Hoare-rules so that we can deal explicitly
   778   with total correctness and non-terminantion, rather than have
   783   with total correctness and non-terminantion, rather than have
   779   notions for partial correctness and termination. Although the latter
   784   notions for partial correctness and termination. Although the latter
   780   would allow us to reason more uniformly (only using Hoare-triples),
   785   would allow us to reason more uniformly (only using Hoare-triples),
   853   \noindent
   858   \noindent
   854   The first is by a simple calculation. The second is by an induction on the
   859   The first is by a simple calculation. The second is by an induction on the
   855   number of steps we can perform starting from the input tape.
   860   number of steps we can perform starting from the input tape.
   856 
   861 
   857   The program @{term tcopy} defined in \eqref{tcopy} has 15 states;
   862   The program @{term tcopy} defined in \eqref{tcopy} has 15 states;
   858   its purpose is to produce the standard tape @{term "(DUMMY, <(n,
   863   its purpose is to produce the standard tape @{term "(Bks, <(n,
   859   n::nat)>)"} when started with @{term "(DUMMY, <(n::nat)>)"}, that is
   864   n::nat)>)"} when started with @{term "(Bks, <(n::nat)>)"}, that is
   860   making a copy of a value on the tape.  Reasoning about this program
   865   making a copy of a value on the tape.  Reasoning about this program
   861   is substantially harder than about @{term dither}. To ease the
   866   is substantially harder than about @{term dither}. To ease the
   862   burden, we derive the following two Hoare-rules for sequentially
   867   burden, we derive the following two Hoare-rules for sequentially
   863   composed programs.
   868   composed programs.
   864 
   869 
   887   The Hoare-rules allow us to prove the correctness of @{term
   892   The Hoare-rules allow us to prove the correctness of @{term
   888   tcopy} by considering the correctness of the components @{term
   893   tcopy} by considering the correctness of the components @{term
   889   "tcopy_begin"}, @{term "tcopy_loop"} and @{term "tcopy_end"} in
   894   "tcopy_begin"}, @{term "tcopy_loop"} and @{term "tcopy_end"} in
   890   isolation. We will show the details for the program @{term
   895   isolation. We will show the details for the program @{term
   891   "tcopy_begin"}. Given the invariants @{term "inv_begin0"},\ldots,
   896   "tcopy_begin"}. Given the invariants @{term "inv_begin0"},\ldots,
   892   @{term "inv_begin4"} shown in Figure~\ref{invbegin} corresponding to 
   897   @{term "inv_begin4"} shown in Figure~\ref{invbegin}, which correspond to 
   893   each state of @{term tcopy_begin}, we define the
   898   each state of @{term tcopy_begin}, we define the
   894   following invariant for the whole @{term tcopy_begin} program:
   899   following invariant for the whole @{term tcopy_begin} program:
   895 
   900 
   896   \begin{figure}
   901   \begin{figure}[t]
   897   \begin{center}
   902   \begin{center}
   898   \begin{tabular}{@ {}lcl@ {\hspace{-2cm}}l@ {}}
   903   \begin{tabular}{@ {}lcl@ {\hspace{-2cm}}l@ {}}
   899   @{thm (lhs) inv_begin0.simps} & @{text "\<equiv>"} & @{thm (rhs) inv_begin01} @{text "\<or>"}& (halting state)\\
   904   @{thm (lhs) inv_begin0.simps} & @{text "\<equiv>"} & @{thm (rhs) inv_begin01} @{text "\<or>"}& (halting state)\\
   900                                 &             & @{thm (rhs) inv_begin02} \\
   905                                 &             & @{thm (rhs) inv_begin02} \\
   901   @{thm (lhs) inv_begin1.simps} & @{text "\<equiv>"} & @{thm (rhs) inv_begin1.simps} & (starting state)\\
   906   @{thm (lhs) inv_begin1.simps} & @{text "\<equiv>"} & @{thm (rhs) inv_begin1.simps} & (starting state)\\
   923   \end{tabular}
   928   \end{tabular}
   924   \end{center}
   929   \end{center}
   925 
   930 
   926   \noindent
   931   \noindent
   927   This invariant depends on @{term n} representing the number of
   932   This invariant depends on @{term n} representing the number of
   928   @{term Oc}s on the tape. It is not hard (26 lines of automated proof
   933   @{term Oc}s (or encoded number) on the tape. It is not hard (26
   929   script) to show that for @{term "n > (0::nat)"} this invariant is
   934   lines of automated proof script) to show that for @{term "n >
   930   preserved under computation rules @{term step} and @{term steps}.
   935   (0::nat)"} this invariant is preserved under computation rules
       
   936   @{term step} and @{term steps}.
   931 
   937 
   932   measure for the copying TM, which we however omit.
   938   measure for the copying TM, which we however omit.
   933 
   939 
   934   
   940   
   935   \begin{center}
   941   \begin{center}