Paper/Paper.thy
changeset 79 bc54c5e648d7
parent 78 2669235c4b1e
child 80 eb589fa73fc1
equal deleted inserted replaced
78:2669235c4b1e 79:bc54c5e648d7
    90 %formalise in Isabelle/HOL computability arguments about the
    90 %formalise in Isabelle/HOL computability arguments about the
    91 %algorithms. 
    91 %algorithms. 
    92 
    92 
    93 
    93 
    94 \noindent
    94 \noindent
    95 Suppose you want to mechanise a proof for whether a predicate @{term P}, say, is
    95 Suppose you want to mechanise a proof for whether a predicate @{term
    96 decidable or not. Decidability of @{text P} usually amounts to showing
    96 P}, say, is decidable or not. Decidability of @{text P} usually
    97 whether \mbox{@{term "P \<or> \<not>P"}} holds. But this does \emph{not} work
    97 amounts to showing whether \mbox{@{term "P \<or> \<not>P"}} holds. But this
    98 in Isabelle/HOL and other HOL theorem provers, since they are based on classical logic
    98 does \emph{not} work in Isabelle/HOL and other HOL theorem provers,
    99 where the law of excluded middle ensures that \mbox{@{term "P \<or> \<not>P"}}
    99 since they are based on classical logic where the law of excluded
   100 is always provable no matter whether @{text P} is constructed by
   100 middle ensures that \mbox{@{term "P \<or> \<not>P"}} is always provable no
   101 computable means. We hit this limitation previously when we mechanised the correctness
   101 matter whether @{text P} is constructed by computable means. We hit on
   102 proofs of two algorithms \cite{UrbanCheneyBerghofer11,WuZhangUrban12}, 
   102 this limitation previously when we mechanised the correctness proofs
   103 but were unable to formalise arguments about decidability.
   103 of two algorithms \cite{UrbanCheneyBerghofer11,WuZhangUrban12}, but
       
   104 were unable to formalise arguments about decidability.
   104 
   105 
   105 %The same problem would arise if we had formulated
   106 %The same problem would arise if we had formulated
   106 %the algorithms as recursive functions, because internally in
   107 %the algorithms as recursive functions, because internally in
   107 %Isabelle/HOL, like in all HOL-based theorem provers, functions are
   108 %Isabelle/HOL, like in all HOL-based theorem provers, functions are
   108 %represented as inductively defined predicates too.
   109 %represented as inductively defined predicates too.
   136 similar to Basic programs involving the infamous goto \cite{Dijkstra68}. This precludes in the
   137 similar to Basic programs involving the infamous goto \cite{Dijkstra68}. This precludes in the
   137 general case a compositional Hoare-style reasoning about Turing
   138 general case a compositional Hoare-style reasoning about Turing
   138 programs.  We provide such Hoare-rules for when it \emph{is} possible to
   139 programs.  We provide such Hoare-rules for when it \emph{is} possible to
   139 reason in a compositional manner (which is fortunately quite often), but also tackle 
   140 reason in a compositional manner (which is fortunately quite often), but also tackle 
   140 the more complicated case when we translate abacus programs into 
   141 the more complicated case when we translate abacus programs into 
   141 Turing programs.  These difficulties when reasoning about computability theory 
   142 Turing programs.  This reasoning about Turing machine programs is
   142 are usually completely left out in the informal literature, e.g.~\cite{Boolos87}.
   143 usually completely left out in the informal literature, e.g.~\cite{Boolos87}.
   143 
   144 
   144 %To see the difficulties
   145 %To see the difficulties
   145 %involved with this work, one has to understand that interactive
   146 %involved with this work, one has to understand that interactive
   146 %theorem provers, like Isabelle/HOL, are at their best when the
   147 %theorem provers, like Isabelle/HOL, are at their best when the
   147 %data-structures at hand are ``structurally'' defined, like lists,
   148 %data-structures at hand are ``structurally'' defined, like lists,
   222 occupied cells. We mechanise the undecidability of the halting problem and
   223 occupied cells. We mechanise the undecidability of the halting problem and
   223 prove the correctness of concrete Turing machines that are needed
   224 prove the correctness of concrete Turing machines that are needed
   224 in this proof; such correctness proofs are left out in the informal literature.  
   225 in this proof; such correctness proofs are left out in the informal literature.  
   225 For reasoning about Turing machine programs we derive Hoare-rules.
   226 For reasoning about Turing machine programs we derive Hoare-rules.
   226 We also construct the universal Turing machine from \cite{Boolos87} by
   227 We also construct the universal Turing machine from \cite{Boolos87} by
   227 relating recursive functions to abacus machines and abacus machines to
   228 translating recursive functions to abacus machines and abacus machines to
   228 Turing machines. Since we have set up in Isabelle/HOL a very general computability
   229 Turing machines. Since we have set up in Isabelle/HOL a very general computability
   229 model and undecidability result, we are able to formalise other
   230 model and undecidability result, we are able to formalise other
   230 results: we describe a proof of the computational equivalence
   231 results: we describe a proof of the computational equivalence
   231 of single-sided Turing machines, which is not given in \cite{Boolos87},
   232 of single-sided Turing machines, which is not given in \cite{Boolos87},
   232 but needed for formalising the undecidability proof of Wang's tiling problem. {\it citation}
   233 but needed for formalising the undecidability proof of Wang's tiling problem. {\it citation}
   277   
   278   
   278   \noindent
   279   \noindent
   279   Note that by using lists each side of the tape is only finite. The
   280   Note that by using lists each side of the tape is only finite. The
   280   potential infinity is achieved by adding an appropriate blank or occupied cell 
   281   potential infinity is achieved by adding an appropriate blank or occupied cell 
   281   whenever the head goes over the ``edge'' of the tape. To 
   282   whenever the head goes over the ``edge'' of the tape. To 
   282   make this formal we define five possible \emph{actions}, @{text a} 
   283   make this formal we define five possible \emph{actions} 
   283   the Turing machine can perform:
   284   the Turing machine can perform:
   284 
   285 
   285   \begin{center}
   286   \begin{center}
   286   \begin{tabular}[t]{@ {}rcl@ {\hspace{2mm}}l}
   287   \begin{tabular}[t]{@ {}rcl@ {\hspace{2mm}}l}
   287   @{text "a"} & $::=$  & @{term "W0"} & (write blank, @{term Bk})\\
   288   @{text "a"} & $::=$  & @{term "W0"} & (write blank, @{term Bk})\\
   382   \label{dither}
   383   \label{dither}
   383   \end{equation}
   384   \end{equation}
   384 
   385 
   385   \noindent
   386   \noindent
   386   the reader can see we have organised our Turing machine programs so
   387   the reader can see we have organised our Turing machine programs so
   387   that segments of two belong to a state. The first component of the
   388   that segments of two belong to a state. The first component of such a
   388   segment determines what action should be taken and which next state
   389   segment determines what action should be taken and which next state
   389   should be transitioned to in case the head reads a @{term Bk};
   390   should be transitioned to in case the head reads a @{term Bk};
   390   similarly the second component determines what should be done in
   391   similarly the second component determines what should be done in
   391   case of reading @{term Oc}. We have the convention that the first
   392   case of reading @{term Oc}. We have the convention that the first
   392   state is always the \emph{starting state} of the Turing machine.
   393   state is always the \emph{starting state} of the Turing machine.
   449   thus moving all ``regular'' states to the segment starting at @{text
   450   thus moving all ``regular'' states to the segment starting at @{text
   450   n}; the second adds @{term "Suc(length p div 2)"} to the @{text
   451   n}; the second adds @{term "Suc(length p div 2)"} to the @{text
   451   0}-state, thus redirecting all references to the ``halting state''
   452   0}-state, thus redirecting all references to the ``halting state''
   452   to the first state after the program @{text p}.  With these two
   453   to the first state after the program @{text p}.  With these two
   453   functions in place, we can define the \emph{sequential composition}
   454   functions in place, we can define the \emph{sequential composition}
   454   of two Turing machine programs @{text "p\<^isub>1"} and @{text "p\<^isub>2"}
   455   of two Turing machine programs @{text "p\<^isub>1"} and @{text "p\<^isub>2"} as
   455 
   456 
   456   \begin{center}
   457   \begin{center}
   457   @{thm tm_comp.simps[where ?p1.0="p\<^isub>1" and ?p2.0="p\<^isub>2", THEN eq_reflection]}
   458   @{thm tm_comp.simps[where ?p1.0="p\<^isub>1" and ?p2.0="p\<^isub>2", THEN eq_reflection]}
   458   \end{center}
   459   \end{center}
   459 
   460 
   467 
   468 
   468   A \emph{configuration} @{term c} of a Turing machine is a state
   469   A \emph{configuration} @{term c} of a Turing machine is a state
   469   together with a tape. This is written as @{text "(s, (l, r))"}.  We
   470   together with a tape. This is written as @{text "(s, (l, r))"}.  We
   470   say a configuration \emph{is final} if @{term "s = (0::nat)"} and we
   471   say a configuration \emph{is final} if @{term "s = (0::nat)"} and we
   471   say a predicate @{text P} \emph{holds for} a configuration if @{text
   472   say a predicate @{text P} \emph{holds for} a configuration if @{text
   472   "P (l, r)"} holds.  If we have a configuration and a program, we can
   473   "P"} holds for the tape @{text "(l, r)"}.  If we have a configuration and a program, we can
   473   calculate what the next configuration is by fetching the appropriate
   474   calculate what the next configuration is by fetching the appropriate
   474   action and next state from the program, and by updating the state
   475   action and next state from the program, and by updating the state
   475   and tape accordingly.  This single step of execution is defined as
   476   and tape accordingly.  This single step of execution is defined as
   476   the function @{term step}
   477   the function @{term step}
   477 
   478 
   483   \end{center}
   484   \end{center}
   484 
   485 
   485   \noindent
   486   \noindent
   486   where @{term "read r"} returns the head of the list @{text r}, or if @{text r} is
   487   where @{term "read r"} returns the head of the list @{text r}, or if @{text r} is
   487   empty it returns @{term Bk}.
   488   empty it returns @{term Bk}.
   488   It is impossible in Isabelle/HOL to lift the @{term step}-function realising
   489   It is impossible in Isabelle/HOL to lift the @{term step}-function to realise
   489   a general evaluation function for Turing machines. The reason is that functions in HOL-based
   490   a general evaluation function for Turing machines. The reason is that functions in HOL-based
   490   provers need to be terminating, and clearly there are Turing machine 
   491   provers need to be terminating, and clearly there are Turing machine 
   491   programs that are not. We can however define an evaluation
   492   programs that are not. We can however define an evaluation
   492   function so that it performs exactly @{text n} steps:
   493   function that performs exactly @{text n} steps:
   493 
   494 
   494   \begin{center}
   495   \begin{center}
   495   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   496   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   496   @{thm (lhs) steps.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(1)}\\
   497   @{thm (lhs) steps.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(1)}\\
   497   @{thm (lhs) steps.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(2)}\\
   498   @{thm (lhs) steps.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(2)}\\
   519   \end{tabular}
   520   \end{tabular}
   520   \end{center}
   521   \end{center}
   521   \caption{Copy machine}\label{copy}
   522   \caption{Copy machine}\label{copy}
   522   \end{figure}
   523   \end{figure}
   523 
   524 
   524   Before we can prove the undecidability of the halting problem for
   525   {\it tapes in standard form}
       
   526 
       
   527   Before we can prove the undecidability of the halting problem for our
   525   Turing machines, we need to analyse two concrete Turing machine
   528   Turing machines, we need to analyse two concrete Turing machine
   526   programs and establish that they are correct, that means they are
   529   programs and establish that they are correct---that means they are
   527   ``doing what they are supposed to be doing''.  This is usually left
   530   ``doing what they are supposed to be doing''.  Such correctness proofs are usually left
   528   out in the informal literature, for example \cite{Boolos87}.  One program 
   531   out in the informal literature, for example \cite{Boolos87}.  One program 
   529   we need to prove correct is the @{term dither} program shown in \eqref{dither} 
   532   we need to prove correct is the @{term dither} program shown in \eqref{dither} 
   530   and the other program @{term "tcopy"} is defined as
   533   and the other program is @{term "tcopy"} is defined as
   531 
   534 
   532   \begin{center}
   535   \begin{center}
   533   \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   536   \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   534   @{thm (lhs) tcopy_def} & @{text "\<equiv>"} & @{thm (rhs) tcopy_def}
   537   @{thm (lhs) tcopy_def} & @{text "\<equiv>"} & @{thm (rhs) tcopy_def}
   535   \end{tabular}
   538   \end{tabular}
   536   \end{center}
   539   \end{center}
   537 
   540 
   538   \noindent
   541   \noindent
   539   whose three components are given in Figure~\ref{copy}. To prove correctness, 
   542   whose three components are given in Figure~\ref{copy}. To the prove
   540   we introduce the notion of total correctness defined in terms of 
   543   correctness of these Turing machine programs, we introduce the
   541   \emph{Hoare-triples}, written @{term "{P} p {Q}"}. They realise the idea
   544   notion of total correctness defined in terms of
   542   that a program @{term p} started in state @{term "1::nat"} with a tape 
   545   \emph{Hoare-triples}, written @{term "{P} p {Q}"}. They realise the
   543   satisfying @{term P} will after @{text n} steps halt (have transitioned into 
   546   idea that a program @{term p} started in state @{term "1::nat"} with
   544   the halting state) with a tape satisfying @{term Q}. We also have 
   547   a tape satisfying @{term P} will after @{text n} steps halt (have
   545   \emph{Hoare-pairs} of the form @{term "{P} p \<up>"} realising the case that a 
   548   transitioned into the halting state) with a tape satisfying @{term
   546   program @{term p} started with a tape satisfying @{term P} will loop 
   549   Q}. We also have \emph{Hoare-pairs} of the form @{term "{P} p \<up>"}
   547   (never transition into the halting state). Both notion are formally 
   550   realising the case that a program @{term p} started with a tape
   548   defined as
   551   satisfying @{term P} will loop (never transition into the halting
       
   552   state). Both notion are formally defined as
   549 
   553 
   550   \begin{center}
   554   \begin{center}
   551   \begin{tabular}{@ {}c@ {\hspace{4mm}}c@ {}}
   555   \begin{tabular}{@ {}c@ {\hspace{4mm}}c@ {}}
   552   \begin{tabular}[t]{@ {}l@ {}}
   556   \begin{tabular}[t]{@ {}l@ {}}
   553   @{thm (lhs) Hoare_halt_def} @{text "\<equiv>"}\\[1mm]
   557   @{thm (lhs) Hoare_halt_def} @{text "\<equiv>"}\\[1mm]
   565   \end{tabular}
   569   \end{tabular}
   566   \end{tabular}
   570   \end{tabular}
   567   \end{center}
   571   \end{center}
   568   
   572   
   569   \noindent
   573   \noindent
   570   We have set up our Hoare-style reasoning to deal explicitly with 
   574   We have set up our Hoare-style reasoning so that we can deal explicitly 
   571   looping and total correctness, rather separate notions for partial 
   575   with looping and total correctness, rather than have notions for partial 
   572   correctness and termination, is because we can derive simple rules 
   576   correctness and termination. Although the latter would allow us to reason
   573   for sequentially composed Turing programs. They allow us to reason
   577   more uniformly (only using Hoare-triples), we prefer our definitions because 
   574   about correctness of components completely separately.
   578   we can derive simple Hoare-rules for sequentially composed Turing programs. 
       
   579   In this way we can reason about the correctness of @{term "tcopy_init"},
       
   580   for example, completely separately from @{term "tcopy_loop"}.
   575 
   581 
   576   It is rather straightforward to prove that the Turing program 
   582   It is rather straightforward to prove that the Turing program 
   577   @{term "dither"} satisfies the following correctness properties
   583   @{term "dither"} satisfies the following correctness properties
   578 
   584 
   579   \begin{center}
   585   \begin{center}