Paper.thy
changeset 30 ba789a0768a2
parent 29 1569a56bd81b
child 31 4ef4b25e2997
equal deleted inserted replaced
29:1569a56bd81b 30:ba789a0768a2
   190 *}
   190 *}
   191 
   191 
   192 section {* Turing Machines *}
   192 section {* Turing Machines *}
   193 
   193 
   194 text {* \noindent
   194 text {* \noindent
   195   Turing machines can be thought of as having a read-write-unit
   195   Turing machines can be thought of as having a read-write-unit, also
       
   196   referred to as \emph{head},
   196   ``gliding'' over a potentially infinite tape. Boolos et
   197   ``gliding'' over a potentially infinite tape. Boolos et
   197   al~\cite{Boolos87} only consider tapes with cells being either blank
   198   al~\cite{Boolos87} only consider tapes with cells being either blank
   198   or occupied, which we represent by a datatype having two
   199   or occupied, which we represent by a datatype having two
   199   constructors, namely @{text Bk} and @{text Oc}.  One way to
   200   constructors, namely @{text Bk} and @{text Oc}.  One way to
   200   represent such tapes is to use a pair of lists, written @{term "(l,
   201   represent such tapes is to use a pair of lists, written @{term "(l,
   201   r)"}, where @{term l} stands for the tape on the left-hand side of the
   202   r)"}, where @{term l} stands for the tape on the left-hand side of the
   202   read-write-unit and @{term r} for the tape on the right-hand side. We have the
   203   head and @{term r} for the tape on the right-hand side. We have the
   203   convention that the head, written @{term hd}, of the right-list is
   204   convention that the head, written @{term hd}, of the right-list is
   204   the cell on which the read-write-unit currently operates. This can
   205   the cell on which the head of the Turing machine currently operates. This can
   205   be pictured as follows:
   206   be pictured as follows:
   206 
   207 
   207   \begin{center}
   208   \begin{center}
   208   \begin{tikzpicture}
   209   \begin{tikzpicture}
   209   \draw[very thick] (-3.0,0)   -- ( 3.0,0);
   210   \draw[very thick] (-3.0,0)   -- ( 3.0,0);
   250   \noindent
   251   \noindent
   251   We slightly deviate
   252   We slightly deviate
   252   from the presentation in \cite{Boolos87} by using the @{term Nop} operation; however its use
   253   from the presentation in \cite{Boolos87} by using the @{term Nop} operation; however its use
   253   will become important when we formalise universal Turing 
   254   will become important when we formalise universal Turing 
   254   machines later. Given a tape and an action, we can define the
   255   machines later. Given a tape and an action, we can define the
   255   following updating function:
   256   following tape updating function:
   256 
   257 
   257   \begin{center}
   258   \begin{center}
   258   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   259   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   259   @{thm (lhs) new_tape_def2(1)} & @{text "\<equiv>"} & @{thm (rhs) new_tape_def2(1)}\\
   260   @{thm (lhs) new_tape_def2(1)} & @{text "\<equiv>"} & @{thm (rhs) new_tape_def2(1)}\\
   260   @{thm (lhs) new_tape_def2(2)} & @{text "\<equiv>"} & @{thm (rhs) new_tape_def2(2)}\\
   261   @{thm (lhs) new_tape_def2(2)} & @{text "\<equiv>"} & @{thm (rhs) new_tape_def2(2)}\\
   266   \end{tabular}
   267   \end{tabular}
   267   \end{center}
   268   \end{center}
   268 
   269 
   269   \noindent
   270   \noindent
   270   The first two clauses replace the head of the right-list
   271   The first two clauses replace the head of the right-list
   271   with new a @{term Bk} or @{term Oc}, repsectively. To see that
   272   with a new @{term Bk} or @{term Oc}, repsectively. To see that
   272   these tow clauses make sense in case where @{text r} is the empty
   273   these two clauses make sense in case where @{text r} is the empty
   273   list, one has to know that the tail function, @{term tl}, is defined
   274   list, one has to know that the tail function, @{term tl}, is in
       
   275   Isabelle/HOL defined
   274   such that @{term "tl [] == []"} holds. The third clause 
   276   such that @{term "tl [] == []"} holds. The third clause 
   275   implements the move of the read-write unit one step to the left: we need
   277   implements the move of the head one step to the left: we need
   276   to test if the left-list @{term l} is empty; if yes, then we just prepend a 
   278   to test if the left-list @{term l} is empty; if yes, then we just prepend a 
   277   blank cell to the right-list; otherwise we have to remove the
   279   blank cell to the right-list; otherwise we have to remove the
   278   head from the left-list and prepend it to the right-list. Similarly
   280   head from the left-list and prepend it to the right-list. Similarly
   279   in the clause for a right move action. The @{term Nop} operation
   281   in the clause for a right move action. The @{term Nop} operation
   280   leaves the the tape unchanged.
   282   leaves the the tape unchanged.
   281 
   283 
   282   Note that our treatment of the tape is rather ``unsymmetric''---we
   284   Note that our treatment of the tape is rather ``unsymmetric''---we
   283   have the convention that the head of the right-list is where the
   285   have the convention that the head of the right-list is where the
   284   read-write unit is currently positioned. Asperti and Ricciotti
   286   head is currently positioned. Asperti and Ricciotti
   285   \cite{AspertiRicciotti12} also consider such a representation, but
   287   \cite{AspertiRicciotti12} also consider such a representation, but
   286   dismiss it as it complicates their definition for \emph{tape
   288   dismiss it as it complicates their definition for \emph{tape
   287   equality}. The reason is that moving the read-write unit one step to
   289   equality}. The reason is that moving the head one step to
   288   the left and then back to the right might change the tape (in case
   290   the left and then back to the right might change the tape (in case
   289   of going over the ``edge''). Therefore they distinguish four types
   291   of going over the ``edge''). Therefore they distinguish four types
   290   of tapes: one where the tape is empty; another where the read-write
   292   of tapes: one where the tape is empty; another where the head
   291   unit is on the left edge, respectively right edge, and in the middle
   293   is on the left edge, respectively right edge, and in the middle
   292   of the tape. The reading, writing and moving of the tape is then
   294   of the tape. The reading, writing and moving of the tape is then
   293   defined in terms of these four cases.  In this way they can keep the
   295   defined in terms of these four cases.  In this way they can keep the
   294   tape in a ``normalised'' form, and thus making a left-move followed
   296   tape in a ``normalised'' form, and thus making a left-move followed
   295   by a right-move being the identity on tapes. Since we are not using
   297   by a right-move being the identity on tapes. Since we are not using
   296   the notion of tape equality, we can get away with the unsymmetric
   298   the notion of tape equality, we can get away with the unsymmetric
   297   definition above and by using the @{term update} function
   299   definition above, and by using the @{term update} function
   298   cover uniformely all cases including the corner cases.
   300   cover uniformely all cases including corner cases.
   299 
   301 
   300   Next we need to define the \emph{states} of a Turing machine.  Given
   302   Next we need to define the \emph{states} of a Turing machine.  Given
   301   how little is usually said about how to represent them in informal
   303   how little is usually said about how to represent them in informal
   302   presentaions, it might be surprising that in a theorem prover we have 
   304   presentations, it might be surprising that in a theorem prover we have 
   303   to select carfully a representation. If we use the naive representation
   305   to select carfully a representation. If we use the naive representation
   304   where a Turing machine consists of a finite set of states, then we
   306   where a Turing machine consists of a finite set of states, then we
   305   will have difficulties composing two Turing machines. In this case we 
   307   will have difficulties composing two Turing machines. In this case we 
   306   would need to combine two finite sets of states, possibly requiring 
   308   would need to combine two finite sets of states, possibly requiring 
   307   renaming states apart whenever both machines share states. This 
   309   renaming states apart whenever both machines share states. This 
   309   the choice of representing a state by a natural number and the states 
   311   the choice of representing a state by a natural number and the states 
   310   of a Turing machine will always consist of the initial segment
   312   of a Turing machine will always consist of the initial segment
   311   of natural numbers starting from @{text 0} up to number of states
   313   of natural numbers starting from @{text 0} up to number of states
   312   of the machine minus @{text 1}. In doing so we can compose
   314   of the machine minus @{text 1}. In doing so we can compose
   313   two Turing machine by ``shifting'' the states of one by an appropriate
   315   two Turing machine by ``shifting'' the states of one by an appropriate
   314   amount to a higher segment.
   316   amount to a higher segment and adjust some ``next states''.
   315 
   317 
   316   An \emph{instruction} @{term i} of a Turing machine is a pair consisting of a
   318   An \emph{instruction} @{term i} of a Turing machine is a pair consisting of 
   317   natural number (the next state) and an action. A \emph{program} @{term p} of a Turing
   319   an action and a natural number (the next state). A \emph{program} @{term p} of a Turing
   318   machine is then a list of such pairs. We have organised our programs
   320   machine is then a list of such pairs. Using the following Turing machine
   319   such that 
   321   program (consisting of four instructions) as an example
   320 
   322 
   321   \begin{center}
   323   \begin{center}
   322   XXX
   324   \begin{tikzpicture}
   323   \end{center}
   325   \node [anchor=base] at (0,0) {@{thm dither_def}};
   324 
   326   \node [anchor=west] at (-1.5,-0.42) {$\underbrace{\hspace{21mm}}_{\text{1st state}}$};
   325   \noindent
   327   \node [anchor=west] at ( 1.1,-0.42) {$\underbrace{\hspace{17mm}}_{\text{2nd state}}$};
       
   328   \node [anchor=west] at (-1.5,0.65) {$\overbrace{\hspace{10mm}}^{\text{@{term Bk}-case}}$};
       
   329   \node [anchor=west] at (-0.1,0.65) {$\overbrace{\hspace{6mm}}^{\text{@{term Oc}-case}}$};
       
   330   \end{tikzpicture}
       
   331   \end{center}
       
   332 
       
   333   \noindent
       
   334   the reader can see we have organised our Turing machine programs so 
       
   335   that segments of two belong to a state. The first component 
       
   336   of the segment determines what action should be taken and which next
       
   337   state should be transitioned to in case the head read a @{term Bk};
       
   338   similarly the second component determines what should be done in
       
   339   case of reading @{term Oc}. We have the convention that the
       
   340   first state is always the \emph{starting state} of the Turing machine. 
       
   341   The 0-state is special in that it will be used as \emph{halting state}.
       
   342   There are no instructions for the 0-state, but it will always 
       
   343   perform a @{term Nop}-operation and remain in the 0-state.
       
   344 
   326   Given a program @{term p}, a state
   345   Given a program @{term p}, a state
   327   and the cell being read by the read-write unit, we need to fetch
   346   and the cell being read by the head, we need to fetch
   328   the corresponding instruction from the program. For this we define 
   347   the corresponding instruction from the program. For this we define 
   329   the function @{term fetch}
   348   the function @{term fetch}
   330  
   349  
   331   \begin{center}
   350   \begin{center}
   332   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   351   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   340   \multicolumn{3}{@ {\hspace{1.4cm}}l}{@{text "None \<Rightarrow> (Nop, 0) |"}}\\
   359   \multicolumn{3}{@ {\hspace{1.4cm}}l}{@{text "None \<Rightarrow> (Nop, 0) |"}}\\
   341   \multicolumn{3}{@ {\hspace{1.4cm}}l}{@{text "Some i \<Rightarrow> i"}}
   360   \multicolumn{3}{@ {\hspace{1.4cm}}l}{@{text "Some i \<Rightarrow> i"}}
   342   \end{tabular}
   361   \end{tabular}
   343   \end{center}
   362   \end{center}
   344 
   363 
   345   start state 1 final state 0
   364   \noindent
   346 
       
   347   \begin{center}
       
   348   @{thm dither_def}
       
   349   \end{center}
       
   350 
   365 
   351 
   366 
   352   A \emph{configuration} @{term c} of a Turing machine is a state together with 
   367   A \emph{configuration} @{term c} of a Turing machine is a state together with 
   353   a tape. This is written as the triple @{term "(s, l, r)"}. If we have a 
   368   a tape. This is written as the triple @{term "(s, l, r)"}. If we have a 
   354   configuration and a program, we can calculate
   369   configuration and a program, we can calculate
   355   what the next configuration is by fetching the appropriate next state
   370   what the next configuration is by fetching the appropriate next state
   356   and action from the program. Such a single step of execution can be defined as
   371   and action from the program. Such a single step of execution can be defined as
   357 
   372 
   358   \begin{center}
   373   \begin{center}
   359   @{thm tstep_def2(1)}\\
   374   \begin{tabular}{l}
       
   375   @{thm (lhs) tstep_def2(1)} @{text "\<equiv>"}\\
       
   376   \hspace{10mm}@{text "let (a, s) = fetch p s (read r)"}\\
       
   377   \hspace{10mm}@{text "in (s', update (l, r) a)"}
       
   378   \end{tabular}
   360   \end{center}
   379   \end{center}
   361 
   380 
   362   No evaluator in HOL, because of totality.
   381   No evaluator in HOL, because of totality.
   363 
   382 
   364   \begin{center}
   383   \begin{center}
   365   @{thm steps.simps(1)}\\
   384   \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
   366   @{thm steps.simps(2)}\\
   385   @{thm (lhs) steps.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(1)}\\
       
   386   @{thm (lhs) steps.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) steps.simps(2)}\\
       
   387   \end{tabular}
   367   \end{center}
   388   \end{center}
   368 
   389 
   369   \emph{well-formedness} of a Turing program
   390   \emph{well-formedness} of a Turing program
   370 
   391 
   371   programs halts
   392   programs halts