Paper/Paper.thy
changeset 126 0b302c0b449a
parent 125 1ce74a77fa2a
child 129 c3832c4963c4
equal deleted inserted replaced
125:1ce74a77fa2a 126:0b302c0b449a
     1 (*<*)
     1 (*<*)
     2 theory Paper
     2 theory Paper
     3 imports "../thys/abacus"
     3 imports "../thys/recursive"
     4 begin
     4 begin
     5 
     5 
     6 (*
     6 
     7 hide_const (open) s 
     7 hide_const (open) s 
     8 *)
     8 
     9 
     9 
    10 
    10 
    11 hide_const (open) Divides.adjust
    11 hide_const (open) Divides.adjust
    12 
    12 
    13 abbreviation
    13 abbreviation
    83   inv_loop0 ("J\<^isub>0") and
    83   inv_loop0 ("J\<^isub>0") and
    84   inv_end1 ("K\<^isub>1") and
    84   inv_end1 ("K\<^isub>1") and
    85   inv_end0 ("K\<^isub>0") and
    85   inv_end0 ("K\<^isub>0") and
    86   measure_begin_step ("M\<^bsub>cbegin\<^esub>") and
    86   measure_begin_step ("M\<^bsub>cbegin\<^esub>") and
    87   layout_of ("layout") and
    87   layout_of ("layout") and
    88   findnth ("find'_nth")
    88   findnth ("find'_nth") and
       
    89   recf.id ("id\<^raw:\makebox[0mm]{\,\,\,\,>\<^isup>_\<^raw:}>\<^isub>_") and
       
    90   Pr ("Pr\<^isup>_") and
       
    91   Cn ("Cn\<^isup>_") and
       
    92   Mn ("Mn\<^isup>_")
    89 
    93 
    90  
    94  
    91 lemma inv_begin_print:
    95 lemma inv_begin_print:
    92   shows "s = 0 \<Longrightarrow> inv_begin n (s, tp) = inv_begin0 n tp" and
    96   shows "s = 0 \<Longrightarrow> inv_begin n (s, tp) = inv_begin0 n tp" and
    93         "s = 1 \<Longrightarrow> inv_begin n (s, tp) = inv_begin1 n tp" and 
    97         "s = 1 \<Longrightarrow> inv_begin n (s, tp) = inv_begin1 n tp" and 
   153 using assms by auto
   157 using assms by auto
   154 
   158 
   155 
   159 
   156 lemma layout:
   160 lemma layout:
   157   shows "layout_of [] = []"
   161   shows "layout_of [] = []"
   158   and   "layout_of ((Inc R\<iota>)#os) = (2 * R\<iota> + 9)#(layout_of os)"
   162   and   "layout_of ((Inc R\<iota>)#is) = (2 * R\<iota> + 9)#(layout_of is)"
   159   and   "layout_of ((Dec R\<iota> i)#os) = (2 * R\<iota> + 16)#(layout_of os)"
   163   and   "layout_of ((Dec R\<iota> l)#is) = (2 * R\<iota> + 16)#(layout_of is)"
   160   and   "layout_of ((Goto i)#os) = 1#(layout_of os)"
   164   and   "layout_of ((Goto l)#is) = 1#(layout_of is)"
   161 by(auto simp add: layout_of.simps length_of.simps)
   165 by(auto simp add: layout_of.simps length_of.simps)
   162 
   166 
   163 
   167 
   164 (*>*)
   168 (*>*)
   165 
   169 
   221 In this paper we take on this daunting prospect and provide a
   225 In this paper we take on this daunting prospect and provide a
   222 formalisation of Turing machines, as well as abacus machines (a kind
   226 formalisation of Turing machines, as well as abacus machines (a kind
   223 of register machines) and recursive functions. To see the difficulties
   227 of register machines) and recursive functions. To see the difficulties
   224 involved with this work, one has to understand that Turing machine
   228 involved with this work, one has to understand that Turing machine
   225 programs can be completely \emph{unstructured}, behaving similar to
   229 programs can be completely \emph{unstructured}, behaving similar to
   226 Basic programs containing the infamous goto-statements \cite{Dijkstra68}. This
   230 Basic programs containing the infamous gotos \cite{Dijkstra68}. This
   227 precludes in the general case a compositional Hoare-style reasoning
   231 precludes in the general case a compositional Hoare-style reasoning
   228 about Turing programs.  We provide such Hoare-rules for when it
   232 about Turing programs.  We provide such Hoare-rules for when it
   229 \emph{is} possible to reason in a compositional manner (which is
   233 \emph{is} possible to reason in a compositional manner (which is
   230 fortunately quite often), but also tackle the more complicated case
   234 fortunately quite often), but also tackle the more complicated case
   231 when we translate abacus programs into Turing programs.  This
   235 when we translate abacus programs into Turing programs.  This
   265 formalization'' \cite[Page 2]{AspertiRicciotti12}. For our
   269 formalization'' \cite[Page 2]{AspertiRicciotti12}. For our
   266 formalisation we follow mainly the proofs from the textbook by Boolos
   270 formalisation we follow mainly the proofs from the textbook by Boolos
   267 et al \cite{Boolos87} and found that the description there is quite
   271 et al \cite{Boolos87} and found that the description there is quite
   268 detailed. Some details are left out however: for example, constructing
   272 detailed. Some details are left out however: for example, constructing
   269 the \emph{copy Turing machine} is left as an excerise to the
   273 the \emph{copy Turing machine} is left as an excerise to the
   270 reader---a correctness proof is not mentioned at all; also \cite{Boolos87}
   274 reader---a corresponding correctness proof is not mentioned at all; also \cite{Boolos87}
   271 only shows how the universal Turing machine is constructed for Turing
   275 only shows how the universal Turing machine is constructed for Turing
   272 machines computing unary functions. We had to figure out a way to
   276 machines computing unary functions. We had to figure out a way to
   273 generalise this result to $n$-ary functions. Similarly, when compiling
   277 generalise this result to $n$-ary functions. Similarly, when compiling
   274 recursive functions to abacus machines, the textbook again only shows
   278 recursive functions to abacus machines, the textbook again only shows
   275 how it can be done for 2- and 3-ary functions, but in the
   279 how it can be done for 2- and 3-ary functions, but in the
   482   \label{dither}
   486   \label{dither}
   483   \end{equation}
   487   \end{equation}
   484   %
   488   %
   485   \noindent
   489   \noindent
   486   the reader can see we have organised our Turing machine programs so
   490   the reader can see we have organised our Turing machine programs so
   487   that segments of two belong to a state. The first component of such a
   491   that segments of two pairs belong to a state. The first component of such a
   488   segment determines what action should be taken and which next state
   492   segment determines what action should be taken and which next state
   489   should be transitioned to in case the head reads a @{term Bk};
   493   should be transitioned to in case the head reads a @{term Bk};
   490   similarly the second component determines what should be done in
   494   similarly the second component determines what should be done in
   491   case of reading @{term Oc}. We have the convention that the first
   495   case of reading @{term Oc}. We have the convention that the first
   492   state is always the \emph{starting state} of the Turing machine.
   496   state is always the \emph{starting state} of the Turing machine.
   725   We often need to restrict tapes to be in standard form, which means 
   729   We often need to restrict tapes to be in standard form, which means 
   726   the left list of the tape is either empty or only contains @{text "Bk"}s, and 
   730   the left list of the tape is either empty or only contains @{text "Bk"}s, and 
   727   the right list contains some ``clusters'' of @{text "Oc"}s separted by single 
   731   the right list contains some ``clusters'' of @{text "Oc"}s separted by single 
   728   blanks. To make this formal we define the following overloaded function
   732   blanks. To make this formal we define the following overloaded function
   729   encoding natural numbers into lists of @{term "Oc"}s and @{term Bk}s.
   733   encoding natural numbers into lists of @{term "Oc"}s and @{term Bk}s.
   730   
   734   % 
   731   \begin{center}
   735   \begin{equation}
   732   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   736   \mbox{\begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   733   @{thm (lhs) nats2tape(6)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(6)}\\
   737   @{thm (lhs) nats2tape(6)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(6)}\\
   734   @{thm (lhs) nats2tape(4)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(4)}\\
   738   @{thm (lhs) nats2tape(4)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(4)}\\
   735   \end{tabular}\hspace{6mm}
   739   \end{tabular}\hspace{6mm}
   736   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   740   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   737   @{thm (lhs) nats2tape(1)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(1)}\\
   741   @{thm (lhs) nats2tape(1)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(1)}\\
   738   @{thm (lhs) nats2tape(2)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(2)}\\
   742   @{thm (lhs) nats2tape(2)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(2)}\\
   739   @{thm (lhs) nats2tape(3)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(3)}
   743   @{thm (lhs) nats2tape(3)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(3)}
   740   \end{tabular}
   744   \end{tabular}}\label{standard}
   741   \end{center}
   745   \end{equation}
   742 
   746   %
   743   \noindent
   747   \noindent
   744   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} 
   748   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} 
   745   and @{text "n\<^isub>i"}. Note that the head in a standard tape ``points'' to the 
   749   and @{text "n\<^bsub>1...m\<^esub>"}. Note that the head in a standard tape ``points'' to the 
   746   leftmost @{term "Oc"} on the tape. Note also that the natural number @{text 0} 
   750   leftmost @{term "Oc"} on the tape. Note also that the natural number @{text 0} 
   747   is represented by a single filled cell on a standard tape, @{text 1} by two filled cells and so on.
   751   is represented by a single filled cell on a standard tape, @{text 1} by two filled cells and so on.
   748 
   752 
   749   Before we can prove the undecidability of the halting problem for
   753   Before we can prove the undecidability of the halting problem for
   750   our Turing machines working on standard tapes, we have to analyse
   754   our Turing machines working on standard tapes, we have to analyse
   818   "tcopy_end"}.
   822   "tcopy_end"}.
   819 
   823 
   820   It is realatively straightforward to prove that the Turing program 
   824   It is realatively straightforward to prove that the Turing program 
   821   @{term "dither"} shown in \eqref{dither} is correct. This program
   825   @{term "dither"} shown in \eqref{dither} is correct. This program
   822   should be the ``identity'' when started with a standard tape representing 
   826   should be the ``identity'' when started with a standard tape representing 
   823   @{text "1"} but loops when started with @{text 0} instead, as pictured 
   827   @{text "1"} but loops when started with the @{text 0}-representation instead, as pictured 
   824   below.
   828   below.
   825 
   829 
   826 
   830 
   827   \begin{center}
   831   \begin{center}
   828   \begin{tabular}{l@ {\hspace{3mm}}lcl}
   832   \begin{tabular}{l@ {\hspace{3mm}}lcl}
   888   number of steps we can perform starting from the input tape.
   892   number of steps we can perform starting from the input tape.
   889 
   893 
   890   The program @{term tcopy} defined in \eqref{tcopy} has 15 states;
   894   The program @{term tcopy} defined in \eqref{tcopy} has 15 states;
   891   its purpose is to produce the standard tape @{term "(Bks, <(n,
   895   its purpose is to produce the standard tape @{term "(Bks, <(n,
   892   n::nat)>)"} when started with @{term "(Bks, <(n::nat)>)"}, that is
   896   n::nat)>)"} when started with @{term "(Bks, <(n::nat)>)"}, that is
   893   making a copy of a value on the tape.  Reasoning about this program
   897   making a copy of a value @{term n} on the tape.  Reasoning about this program
   894   is substantially harder than about @{term dither}. To ease the
   898   is substantially harder than about @{term dither}. To ease the
   895   burden, we derive the following two Hoare-rules for sequentially
   899   burden, we derive the following two Hoare-rules for sequentially
   896   composed programs.
   900   composed programs.
   897 
   901 
   898   \begin{center}
   902   \begin{center}
   969   \end{tabular}
   973   \end{tabular}
   970   \end{center}
   974   \end{center}
   971 
   975 
   972   \noindent
   976   \noindent
   973   This invariant depends on @{term n} representing the number of
   977   This invariant depends on @{term n} representing the number of
   974   @{term Oc}s (or encoded number) on the tape. It is not hard (26
   978   @{term Oc}s@{text "+1"} (or encoded number) on the tape. It is not hard (26
   975   lines of automated proof script) to show that for @{term "n >
   979   lines of automated proof script) to show that for @{term "n >
   976   (0::nat)"} this invariant is preserved under the computation rules
   980   (0::nat)"} this invariant is preserved under the computation rules
   977   @{term step} and @{term steps}. This gives us partial correctness
   981   @{term step} and @{term steps}. This gives us partial correctness
   978   for @{term "tcopy_begin"}. 
   982   for @{term "tcopy_begin"}. 
   979 
   983 
   980   We next need to show that @{term "tcopy_begin"} terminates. For this
   984   We next need to show that @{term "tcopy_begin"} terminates. For this
   981   we introduce lexicographically ordered pairs @{term "(n, m)"}
   985   we introduce lexicographically ordered pairs @{term "(n, m)"}
   982   derived from configurations @{text "(s, (l, r))"}: @{text n} is
   986   derived from configurations @{text "(s, (l, r))"} whereby @{text n} is
   983   the state @{text s}, but ordered according to how @{term tcopy_begin} executes
   987   the state @{text s}, but ordered according to how @{term tcopy_begin} executes
   984   them, that is @{text "1 > 2 > 3 > 4 > 0"}; in order to have
   988   them, that is @{text "1 > 2 > 3 > 4 > 0"}; in order to have
   985   a strictly decreasing meansure, @{term m} takes the data on the tape into
   989   a strictly decreasing meansure, @{term m} takes the data on the tape into
   986   account and is calculated according to the following measure function:
   990   account and is calculated according to the following measure function:
   987 
   991 
   999   \noindent
  1003   \noindent
  1000   With this in place, we can show that for every starting tape of the
  1004   With this in place, we can show that for every starting tape of the
  1001   form @{term "([], Oc \<up> n)"} with @{term "n > (0::nat)"}, the Turing
  1005   form @{term "([], Oc \<up> n)"} with @{term "n > (0::nat)"}, the Turing
  1002   machine @{term "tcopy_begin"} will eventually halt (the measure
  1006   machine @{term "tcopy_begin"} will eventually halt (the measure
  1003   decreases in each step). Taking this and the partial correctness
  1007   decreases in each step). Taking this and the partial correctness
  1004   proof together, we obtain the left-most Hoare-triple for @{term tcopy_begin}:
  1008   proof together, we obtain the Hoare-triple shown on the left for @{term tcopy_begin}:
  1005    
  1009    
  1006 
  1010 
  1007   \begin{center}
  1011   \begin{center}
  1008   @{thm (concl) begin_correct}\hspace{6mm}
  1012   @{thm (concl) begin_correct}\hspace{6mm}
  1009   @{thm (concl) loop_correct}\hspace{6mm}
  1013   @{thm (concl) loop_correct}\hspace{6mm}
  1010   @{thm (concl) end_correct}
  1014   @{thm (concl) end_correct}
  1011   \end{center}
  1015   \end{center}
  1012 
  1016 
  1013   \noindent 
  1017   \noindent 
  1014   where we assume @{text "0 < n"} (similar resoning is needed for
  1018   where we assume @{text "0 < n"} (similar reasoning is needed for
  1015   the Hoare-triples for @{term tcopy_loop} and @{term tcopy_end}). Since the invariant of
  1019   the Hoare-triples for @{term tcopy_loop} and @{term tcopy_end}). Since the invariant of
  1016   the halting state of @{term tcopy_begin} implies the invariant of
  1020   the halting state of @{term tcopy_begin} implies the invariant of
  1017   the starting state of @{term tcopy_loop}, that is @{term "inv_begin0
  1021   the starting state of @{term tcopy_loop}, that is @{term "inv_begin0
  1018   n \<mapsto> inv_loop1 n"} holds, and also @{term "inv_loop0 n = inv_end1
  1022   n \<mapsto> inv_loop1 n"} holds, and also @{term "inv_loop0 n = inv_end1
  1019   n"}, we can derive the following Hoare-triple for the correctness 
  1023   n"}, we can derive the following Hoare-triple for the correctness 
  1040   This roughly means we considering only Turing machine programs
  1044   This roughly means we considering only Turing machine programs
  1041   representing functions that take some numbers as input and produce a
  1045   representing functions that take some numbers as input and produce a
  1042   single number as output. For undecidability, the property we are
  1046   single number as output. For undecidability, the property we are
  1043   proving is that there is no Turing machine that can decide in
  1047   proving is that there is no Turing machine that can decide in
  1044   general whether a Turing machine program halts (answer either @{text
  1048   general whether a Turing machine program halts (answer either @{text
  1045   0} for halting and @{text 1} for looping). Given our correctness
  1049   0} for halting or @{text 1} for looping). Given our correctness
  1046   proofs for @{term dither} and @{term tcopy} shown above, this
  1050   proofs for @{term dither} and @{term tcopy} shown above, this
  1047   non-existence is now relatively straightforward to establish. We first
  1051   non-existence is now relatively straightforward to establish. We first
  1048   assume there is a coding function, written @{term "code M"}, which
  1052   assume there is a coding function, written @{term "code M"}, which
  1049   represents a Turing machine @{term "M"} as a natural number.  No
  1053   represents a Turing machine @{term "M"} as a natural number.  No
  1050   further assumptions are made about this coding function. Suppose a
  1054   further assumptions are made about this coding function. Suppose a
  1051   Turing machine @{term H} exists such that if started with the
  1055   Turing machine @{term H} exists such that if started with the
  1052   standard tape @{term "([Bk], <(code M, ns)>)"} returns @{text 0},
  1056   standard tape @{term "([Bk], <(code M, ns)>)"} returns @{text 0},
  1053   respectively @{text 1}, depending on whether @{text M} halts when
  1057   respectively @{text 1}, depending on whether @{text M} halts or not when
  1054   started with the input tape containing @{term "<ns>"}.  This
  1058   started with the input tape containing @{term "<ns>"}.  This
  1055   assumption is formalised as follows---for all @{term M} and all lists of
  1059   assumption is formalised as follows---for all @{term M} and all lists of
  1056   natural numbers @{term ns}:
  1060   natural numbers @{term ns}:
  1057 
  1061 
  1058   \begin{center}
  1062   \begin{center}
  1071   \begin{center}
  1075   \begin{center}
  1072   @{thm tcontra_def}
  1076   @{thm tcontra_def}
  1073   \end{center}
  1077   \end{center}
  1074 
  1078 
  1075   \noindent
  1079   \noindent
  1076   Suppose @{thm (prem 1) "tcontra_halt"} holds. Given the invariants on the 
  1080   Suppose @{thm (prem 1) "tcontra_halt"} holds. Given the invariants @{text "P\<^isub>1"}\ldots@{text "P\<^isub>3"} 
       
  1081   shown on the 
  1077   left, we can derive the following Hoare-pair for @{term tcontra} on the right.
  1082   left, we can derive the following Hoare-pair for @{term tcontra} on the right.
  1078 
  1083 
  1079   \begin{center}\small
  1084   \begin{center}\small
  1080   \begin{tabular}{@ {}c@ {\hspace{-10mm}}c@ {}}
  1085   \begin{tabular}{@ {}c@ {\hspace{-10mm}}c@ {}}
  1081   \begin{tabular}[t]{@ {}l@ {}}
  1086   \begin{tabular}[t]{@ {}l@ {}}
  1098 
  1103 
  1099   \noindent
  1104   \noindent
  1100   This Hoare-pair contradicts our assumption that @{term tcontra} started
  1105   This Hoare-pair contradicts our assumption that @{term tcontra} started
  1101   with @{term "<(code tcontra)>"} halts.
  1106   with @{term "<(code tcontra)>"} halts.
  1102 
  1107 
  1103   Suppose @{thm (prem 1) "tcontra_unhalt"} holds. Again given the invariants on the 
  1108   Suppose @{thm (prem 1) "tcontra_unhalt"} holds. Again, given the invariants 
       
  1109   @{text "Q\<^isub>1"}\ldots@{text "Q\<^isub>3"}
       
  1110   shown on the 
  1104   left, we can derive the Hoare-triple for @{term tcontra} on the right.
  1111   left, we can derive the Hoare-triple for @{term tcontra} on the right.
  1105 
  1112 
  1106   \begin{center}\small
  1113   \begin{center}\small
  1107   \begin{tabular}{@ {}c@ {\hspace{-18mm}}c@ {}}
  1114   \begin{tabular}{@ {}c@ {\hspace{-18mm}}c@ {}}
  1108   \begin{tabular}[t]{@ {}l@ {}}
  1115   \begin{tabular}[t]{@ {}l@ {}}
  1124   \end{center}
  1131   \end{center}
  1125 
  1132 
  1126   \noindent
  1133   \noindent
  1127   This time the Hoare-triple states that @{term tcontra} terminates 
  1134   This time the Hoare-triple states that @{term tcontra} terminates 
  1128   with the ``output'' @{term "<(1::nat)>"}. In both case we come
  1135   with the ``output'' @{term "<(1::nat)>"}. In both case we come
  1129   to an contradiction, which means we have to abondon our assumption 
  1136   to a contradiction, which means we have to abondon our assumption 
  1130   that there exists a Turing machine @{term H} which can in general decide 
  1137   that there exists a Turing machine @{term H} which can in general decide 
  1131   whether Turing machines terminate.
  1138   whether Turing machines terminate.
  1132 *}
  1139 *}
  1133 
  1140 
  1134 
  1141 
  1136 
  1143 
  1137 text {*
  1144 text {*
  1138   \noindent
  1145   \noindent
  1139   Boolos et al \cite{Boolos87} use abacus machines as a stepping stone
  1146   Boolos et al \cite{Boolos87} use abacus machines as a stepping stone
  1140   for making it less laborious to write Turing machine
  1147   for making it less laborious to write Turing machine
  1141   programs. Abacus machines operate over a set of registers $R_0$,
  1148   programs. Abacus machines operate over a set of registers @{text "R\<^isub>0"},
  1142   $R_1$, \ldots{} each being able to hold an arbitrary large natural
  1149   @{text "R\<^isub>1"}, \ldots{}, @{text "R\<^isub>n"} each being able to hold an arbitrary large natural
  1143   number.  We use natural numbers to refer to registers; we also use a natural number
  1150   number.  We use natural numbers to refer to registers; we also use a natural number
  1144   to represent a program counter and to represent jumping ``addresses''. An abacus 
  1151   to represent a program counter and to represent jumping ``addresses'', for which we 
       
  1152   use the letter @{text l}. An abacus 
  1145   program is a list of \emph{instructions} defined by the datatype:
  1153   program is a list of \emph{instructions} defined by the datatype:
  1146 
  1154 
  1147   \begin{center}
  1155   \begin{center}
  1148   \begin{tabular}{rcl@ {\hspace{10mm}}l}
  1156   \begin{tabular}{rcl@ {\hspace{10mm}}l}
  1149   @{text "i"} & $::=$  & @{term "Inc R\<iota>"} & increment register $R$ by one\\
  1157   @{text "i"} & $::=$  & @{term "Inc R\<iota>"} & increment register $R$ by one\\
  1150   & $\mid$ & @{term "Dec R\<iota> i"} & if content of $R$ is non-zero, then decrement it by one\\
  1158   & $\mid$ & @{term "Dec R\<iota> l"} & if content of $R$ is non-zero, then decrement it by one\\
  1151   & & & otherwise jump to instruction $i$\\
  1159   & & & otherwise jump to instruction $l$\\
  1152   & $\mid$ & @{term "Goto i"} & jump to instruction $i$
  1160   & $\mid$ & @{term "Goto l"} & jump to instruction $l$
  1153   \end{tabular}
  1161   \end{tabular}
  1154   \end{center}
  1162   \end{center}
  1155 
  1163 
  1156   \noindent
  1164   \noindent
  1157   For example the program clearing the register $R$ (that is setting
  1165   For example the program clearing the register @{text R} (that is setting
  1158   it to @{term "(0::nat)"}) can be defined as follows:
  1166   it to @{term "(0::nat)"}) can be defined as follows:
  1159 
  1167 
  1160   \begin{center}
  1168   \begin{center}
  1161   @{thm clear.simps[where n="R\<iota>" and e="i", THEN eq_reflection]}
  1169   @{thm clear.simps[where n="R\<iota>" and e="l", THEN eq_reflection]}
  1162   \end{center}
  1170   \end{center}
  1163 
  1171 
  1164   \noindent
  1172   \noindent
  1165   Running such a program means we start with the first instruction
  1173   Running such a program means we start with the first instruction
  1166   then execute one instructions after the other, unless there is a jump.  For
  1174   then execute one instructions after the other, unless there is a jump.  For
  1167   example the second instruction @{term "Goto 0"} in @{term clear} means
  1175   example the second instruction @{term "Goto 0"} means
  1168   we jump back to the first instruction thereby closing the loop.  Like with our
  1176   we jump back to the first instruction thereby closing the loop.  Like with our
  1169   Turing machines, we fetch instructions from an abacus program such
  1177   Turing machines, we fetch instructions from an abacus program such
  1170   that a jump out of ``range'' behaves like a @{term "Nop"}-action. In
  1178   that a jump out of ``range'' behaves like a @{term "Nop"}-action. In
  1171   this way it is again easy to define a function @{term steps} that
  1179   this way it is again easy to define a function @{term steps} that
  1172   executes @{term n} instructions of an abacus program. A \emph{configuration}
  1180   executes @{term n} instructions of an abacus program. A \emph{configuration}
  1173   of an abacus machine is the current program counter together with a snapshot of 
  1181   of an abacus machine is the current program counter together with a snapshot of 
  1174   all registers.
  1182   all registers.
  1175   By convention
  1183   By convention
  1176   the value calculated by an abacus program is stored in the
  1184   the value calculated by an abacus program is stored in the
  1177   last register (the register with the highest index).
  1185   last register (the one with the highest index in the program).
  1178   
  1186   
  1179   The main point of abacus programs is to be able to translate them to 
  1187   The main point of abacus programs is to be able to translate them to 
  1180   Turing machine programs. Registers and their content are represented by
  1188   Turing machine programs. Registers and their content are represented by
  1181   standard tapes. Because of the jumps in abacus programs, it
  1189   standard tapes (see definition shown in \eqref{standard}). Because of the jumps in abacus programs, it
  1182   seems difficult to build a Turing machine programs out of components 
  1190   is impossible to build a Turing machine programs out of components 
  1183   using our @{text "\<oplus>"}-operation shown in the previous section.
  1191   using our @{text "\<oplus>"}-operation shown in the previous section.
  1184   To overcome this difficulty, we calculate a \emph{layout} of an
  1192   To overcome this difficulty, we calculate a \emph{layout} of an
  1185   abacus program as follows
  1193   abacus program as follows
  1186 
  1194 
  1187   \begin{center}
  1195   \begin{center}
  1193   \end{tabular}
  1201   \end{tabular}
  1194   \end{center}
  1202   \end{center}
  1195 
  1203 
  1196   \noindent
  1204   \noindent
  1197   This gives us a list of natural numbers specifying how many states
  1205   This gives us a list of natural numbers specifying how many states
  1198   are needed to translate each abacus instruction. The @{text Goto}
  1206   are needed to translate each abacus instruction. This information
  1199   instruction is easiest to translate requiring only one state in
  1207   is needed in order to calculate the state where the Turing program
  1200   the corresponding Turing machine:
  1208   code of one abacus instruction ends.
  1201 
  1209   The @{text Goto}
  1202   \begin{center}
  1210   instruction is easiest to translate requiring only one state, namely
  1203   @{thm (rhs) tgoto.simps[where n="i"]}
  1211   the Turing machine program:
  1204   \end{center}
  1212 
  1205 
  1213   \begin{center}
  1206   \noindent
  1214   @{text "tm_of_Goto l"} @{text "\<equiv>"} @{thm (rhs) tgoto.simps[where n="l"]}
  1207   where @{term "i"} is the state in the Turing machine program 
  1215   \end{center}
  1208   to jump to. For translating the instruction @{term Inc}, 
  1216 
       
  1217   \noindent
       
  1218   where @{term "l"} is the state in the Turing machine program 
       
  1219   to jump to. For translating the instruction @{term "Inc R\<iota>"}, 
  1209   one has to remember that the content of the registers are encoded
  1220   one has to remember that the content of the registers are encoded
  1210   in the Turing machine as standard tape. Therefore the translated Turing machine 
  1221   in the Turing machine as a standard tape. Therefore the translated Turing machine 
  1211   needs to first find the number corresponding to the register @{text "R"}. This needs a machine
  1222   needs to first find the number corresponding to the content of register 
       
  1223   @{text "R"}. This needs a machine
  1212   with @{term "(2::nat) * R\<iota>"} states and can be constructed as follows: 
  1224   with @{term "(2::nat) * R\<iota>"} states and can be constructed as follows: 
  1213 
  1225 
  1214   \begin{center}
  1226   \begin{center}
  1215   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
  1227   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
  1216   @{thm (lhs) findnth.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) findnth.simps(1)}\\
  1228   @{thm (lhs) findnth.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) findnth.simps(1)}\\
  1220   \end{center}
  1232   \end{center}
  1221 
  1233 
  1222   \noindent
  1234   \noindent
  1223   Then we need to increase the ``number'' on the tape by one,
  1235   Then we need to increase the ``number'' on the tape by one,
  1224   and adjust the following ``registers''. By adjusting we only need to 
  1236   and adjust the following ``registers''. By adjusting we only need to 
  1225   replace the first @{term Oc} of each number by @{term Bk} and the last
  1237   change the first @{term Oc} of each number to @{term Bk} and the last
  1226   one from @{term Bk} to @{term Oc}.
  1238   one from @{term Bk} to @{term Oc}.
  1227   Finally, we need to transition the head of the
  1239   Finally, we need to transition the head of the
  1228   Turing machine back into the standard position. This requires a Turing machine
  1240   Turing machine back into the standard position. This requires a Turing machine
  1229   with 9 states (we omit the details). Similarly for the translation of @{term Dec}, where the 
  1241   with 9 states (we omit the details). Similarly for the translation of @{term "Dec R\<iota> l"}, where the 
  1230   translated Turing machine needs to first check whether the content of the
  1242   translated Turing machine needs to first check whether the content of the
  1231   corresponding register is @{text 0}. For this we have a Turing machine program
  1243   corresponding register is @{text 0}. For this we have a Turing machine program
  1232   with @{text 16} states (again details are omitted). 
  1244   with @{text 16} states (again details are omitted). 
  1233 
  1245 
  1234   Finally, having a Turing machine for each abacus instruction we need
  1246   Finally, having a Turing machine for each abacus instruction we need
  1235   to ``stitch'' the Turing machines together into one so that each
  1247   to ``stitch'' the Turing machines together into one so that each
  1236   Turing machine component transitions to next one, just like in
  1248   Turing machine component transitions to next one, just like in
  1237   the abacus programs. One last problem to overcome is that an abacus
  1249   the abacus programs. One last problem to overcome is that an abacus
  1238   program is assumed to calculate a value stored in the last
  1250   program is assumed to calculate a value stored in the last
  1239   register. That means we have to append a Turing machine that
  1251   register (the one with the highest register). That means we have to append a Turing machine that
  1240   ``mops up'' the tape (cleaning all @{text Oc}s) except for the
  1252   ``mops up'' the tape (cleaning all @{text Oc}s) except for the
  1241   @{term Oc}s of the last number represented on the tape.
  1253   @{term Oc}s of the last number represented on the tape. This needs
       
  1254   a Turing machine program with @{text "2 * R + 12"} states, assuming @{text R}
       
  1255   is the number of registers to be ``cleaned''.
  1242 
  1256 
  1243   While generating the Turing machine program for an abacus program is
  1257   While generating the Turing machine program for an abacus program is
  1244   not too difficult to formalise, the problem is that it contains
  1258   not too difficult to formalise, the problem is that it contains
  1245   @{text Goto}s all over the place. The unfortunate result is that we
  1259   @{text Goto}s all over the place. The unfortunate result is that we
  1246   cannot use our Hoare-rules for reasoning about sequentially composed
  1260   cannot use our Hoare-rules for reasoning about sequentially composed
  1247   programs (for this the programs need to be independent). Instead we
  1261   programs (for this each component needs to be completely independent). Instead we
  1248   have to treat the translated Turing machine as one ``big block'' and 
  1262   have to treat the translated Turing machine as one ``big block'' and 
  1249   prove as invariant that it performs
  1263   prove as invariant that it performs
  1250   the same operations as the abacus program. For this we have to show
  1264   the same operations as the abacus program. For this we have to show
  1251   that for each configuration of an abacus machine the @{term
  1265   that for each configuration of an abacus machine the @{term
  1252   step}-function is simulated by zero or more steps in our translated
  1266   step}-function is simulated by zero or more steps in our translated
  1259 
  1273 
  1260 
  1274 
  1261 section {* Recursive Functions and a Universal Turing Machine *}
  1275 section {* Recursive Functions and a Universal Turing Machine *}
  1262 
  1276 
  1263 text {*
  1277 text {*
  1264 
  1278   The main point of recursive functions is that we can relatively 
       
  1279   easily construct a universal Turing machine via a universal 
       
  1280   function. This is different from Norrish \cite{Norrish11} who gives a universal 
       
  1281   function for Church numbers, and also from Asperti and Ricciotti 
       
  1282   \cite{AspertiRicciotti12} who construct a universal Turing machine
       
  1283   directly, but for simulating Turing machines with a more restricted alphabet.
       
  1284   \emph{Recursive functions} @{term r} are defined as the datatype
       
  1285 
       
  1286   \begin{center}
       
  1287   \begin{tabular}{c@ {\hspace{4mm}}c}
       
  1288   \begin{tabular}{rcl@ {\hspace{4mm}}l}
       
  1289   @{term r} & @{text "::="} & @{term z} & (zero-functions)\\
       
  1290             & @{text "|"}   & @{term s} & (successor-function)\\
       
  1291             & @{text "|"}   & @{term "id n m"} & (projection)\\
       
  1292   \end{tabular} &
       
  1293   \begin{tabular}{cl@ {\hspace{4mm}}l}
       
  1294   @{text "|"} & @{term "Cn n r rs"} & (composition)\\
       
  1295   @{text "|"} & @{term "Pr n r\<^isub>1 r\<^isub>2"} & (primitive recursion)\\
       
  1296   @{text "|"} & @{term "Mn n r"} & (minimisation)\\
       
  1297   \end{tabular}
       
  1298   \end{tabular}
       
  1299   \end{center}
       
  1300 
       
  1301   \noindent 
       
  1302   where @{text n} indicates the function expects @{term n} arguments
       
  1303   (@{text z} and @{term s} expect one argument), and @{text rs} stands
       
  1304   for a list of recursive functions. Since we know in each case 
       
  1305   the arity, say @{term n}, we can define an inductive relation that  
       
  1306   relates a recursive function and a list of natural numbers of length @{text n},
       
  1307   to what the result of the recurisve function is---we omit the straightforward
       
  1308   definition. Because of space reasons, we also omit the definition of translating
       
  1309   recursive functions into abacus programs and the also the definition of the
       
  1310   universal function (we refer the reader to our formalisation).
       
  1311   
  1265 *}
  1312 *}
  1266 
  1313 
  1267 (*
  1314 (*
  1268 section {* Wang Tiles\label{Wang} *}
  1315 section {* Wang Tiles\label{Wang} *}
  1269 
  1316 
  1273 *)
  1320 *)
  1274 
  1321 
  1275 section {* Conclusion *}
  1322 section {* Conclusion *}
  1276 
  1323 
  1277 text {*
  1324 text {*
  1278   We have formalised the main results from three chapters in the
  1325   We have formalised the main results from six chapters in the
  1279   textbook by Boolos et al \cite{Boolos87}.  Following in the
  1326   textbook by Boolos et al \cite{Boolos87}.  Following in the
  1280   footsteps of another paper \cite{Nipkow98} formalising the results
  1327   footsteps of another paper \cite{Nipkow98} formalising the results
  1281   from a semantics textbook, we could have titled our paper ``Boolos et al are
  1328   from a semantics textbook, we could have titled our paper ``Boolos et al are
  1282   (almost) Right''. We have not attempted to formalise everything
  1329   (almost) Right''. We have not attempted to formalise everything
  1283   precisely as Boolos et al present it, but use definitions that make
  1330   precisely as Boolos et al present it, but use definitions that make
  1349   possible, but sometimes not. In order to ease their reasoning they
  1396   possible, but sometimes not. In order to ease their reasoning they
  1350   introduced a more primitive specification logic, on which
  1397   introduced a more primitive specification logic, on which
  1351   for special cases Hoare-rules can be provided.
  1398   for special cases Hoare-rules can be provided.
  1352   It remains to be seen whether their specification logic
  1399   It remains to be seen whether their specification logic
  1353   for assmebly code can make it easier to reason about our Turing
  1400   for assmebly code can make it easier to reason about our Turing
  1354   programs.
  1401   programs. That would be an attractive result, because Turing 
       
  1402   machine programs are 
       
  1403 
  1355   The code of our formalisation is available from the Mercurial repository at
  1404   The code of our formalisation is available from the Mercurial repository at
  1356   \url{http://www.dcs.kcl.ac.uk/staff/urbanc/cgi-bin/repos.cgi/tm/}
  1405   \url{http://www.dcs.kcl.ac.uk/staff/urbanc/cgi-bin/repos.cgi/tm/}
  1357 *}
  1406 *}
  1358 
  1407 
  1359 
  1408