Paper/Paper.thy
changeset 130 1e89c65f844b
parent 129 c3832c4963c4
child 132 264ff7014657
equal deleted inserted replaced
129:c3832c4963c4 130:1e89c65f844b
    87   layout_of ("layout") and
    87   layout_of ("layout") and
    88   findnth ("find'_nth") and
    88   findnth ("find'_nth") and
    89   recf.id ("id\<^raw:\makebox[0mm]{\,\,\,\,>\<^isup>_\<^raw:}>\<^isub>_") and
    89   recf.id ("id\<^raw:\makebox[0mm]{\,\,\,\,>\<^isup>_\<^raw:}>\<^isub>_") and
    90   Pr ("Pr\<^isup>_") and
    90   Pr ("Pr\<^isup>_") and
    91   Cn ("Cn\<^isup>_") and
    91   Cn ("Cn\<^isup>_") and
    92   Mn ("Mn\<^isup>_")
    92   Mn ("Mn\<^isup>_") and
       
    93   rec_calc_rel ("eval") and 
       
    94   tm_of_rec ("translate")
    93 
    95 
    94  
    96  
    95 lemma inv_begin_print:
    97 lemma inv_begin_print:
    96   shows "s = 0 \<Longrightarrow> inv_begin n (s, tp) = inv_begin0 n tp" and
    98   shows "s = 0 \<Longrightarrow> inv_begin n (s, tp) = inv_begin0 n tp" and
    97         "s = 1 \<Longrightarrow> inv_begin n (s, tp) = inv_begin1 n tp" and 
    99         "s = 1 \<Longrightarrow> inv_begin n (s, tp) = inv_begin1 n tp" and 
   268 ``sufficiently accurate to be directly usable as a guideline for
   270 ``sufficiently accurate to be directly usable as a guideline for
   269 formalization'' \cite[Page 2]{AspertiRicciotti12}. For our
   271 formalization'' \cite[Page 2]{AspertiRicciotti12}. For our
   270 formalisation we follow mainly the proofs from the textbook by Boolos
   272 formalisation we follow mainly the proofs from the textbook by Boolos
   271 et al \cite{Boolos87} and found that the description there is quite
   273 et al \cite{Boolos87} and found that the description there is quite
   272 detailed. Some details are left out however: for example, constructing
   274 detailed. Some details are left out however: for example, constructing
   273 the \emph{copy Turing machine} is left as an excerise to the
   275 the \emph{copy Turing machine} is left as an exercise to the
   274 reader---a corresponding correctness proof is not mentioned at all; also \cite{Boolos87}
   276 reader---a corresponding correctness proof is not mentioned at all; also \cite{Boolos87}
   275 only shows how the universal Turing machine is constructed for Turing
   277 only shows how the universal Turing machine is constructed for Turing
   276 machines computing unary functions. We had to figure out a way to
   278 machines computing unary functions. We had to figure out a way to
   277 generalise this result to $n$-ary functions. Similarly, when compiling
   279 generalise this result to $n$-ary functions. Similarly, when compiling
   278 recursive functions to abacus machines, the textbook again only shows
   280 recursive functions to abacus machines, the textbook again only shows
   347   the head and @{term r} for the tape on the right-hand side.  We use
   349   the head and @{term r} for the tape on the right-hand side.  We use
   348   the notation @{term "Bk \<up> n"} (similarly @{term "Oc \<up> n"}) for lists
   350   the notation @{term "Bk \<up> n"} (similarly @{term "Oc \<up> n"}) for lists
   349   composed of @{term n} elements of @{term Bk}s.  We also have the
   351   composed of @{term n} elements of @{term Bk}s.  We also have the
   350   convention that the head, abbreviated @{term hd}, of the right list
   352   convention that the head, abbreviated @{term hd}, of the right list
   351   is the cell on which the head of the Turing machine currently
   353   is the cell on which the head of the Turing machine currently
   352   scannes. This can be pictured as follows:
   354   scans. This can be pictured as follows:
   353   %
   355   %
   354   \begin{center}
   356   \begin{center}
   355   \begin{tikzpicture}[scale=0.9]
   357   \begin{tikzpicture}[scale=0.9]
   356   \draw[very thick] (-3.0,0)   -- ( 3.0,0);
   358   \draw[very thick] (-3.0,0)   -- ( 3.0,0);
   357   \draw[very thick] (-3.0,0.5) -- ( 3.0,0.5);
   359   \draw[very thick] (-3.0,0.5) -- ( 3.0,0.5);
   547   @{thm (lhs) adjust.simps} @{text "\<equiv>"} @{thm (rhs) adjust.simps}\\
   549   @{thm (lhs) adjust.simps} @{text "\<equiv>"} @{thm (rhs) adjust.simps}\\
   548   \end{tabular}
   550   \end{tabular}
   549   \end{center}
   551   \end{center}
   550 
   552 
   551   \noindent
   553   \noindent
   552   The first adds @{text n} to all states, exept the @{text 0}-state,
   554   The first adds @{text n} to all states, except the @{text 0}-state,
   553   thus moving all ``regular'' states to the segment starting at @{text
   555   thus moving all ``regular'' states to the segment starting at @{text
   554   n}; the second adds @{term "Suc(length p div 2)"} to the @{text
   556   n}; the second adds @{term "Suc(length p div 2)"} to the @{text
   555   0}-state, thus redirecting all references to the ``halting state''
   557   0}-state, thus redirecting all references to the ``halting state''
   556   to the first state after the program @{text p}.  With these two
   558   to the first state after the program @{text p}.  With these two
   557   functions in place, we can define the \emph{sequential composition}
   559   functions in place, we can define the \emph{sequential composition}
   726   \end{figure}
   728   \end{figure}
   727 
   729 
   728 
   730 
   729   We often need to restrict tapes to be in standard form, which means 
   731   We often need to restrict tapes to be in standard form, which means 
   730   the left list of the tape is either empty or only contains @{text "Bk"}s, and 
   732   the left list of the tape is either empty or only contains @{text "Bk"}s, and 
   731   the right list contains some ``clusters'' of @{text "Oc"}s separted by single 
   733   the right list contains some ``clusters'' of @{text "Oc"}s separated by single 
   732   blanks. To make this formal we define the following overloaded function
   734   blanks. To make this formal we define the following overloaded function
   733   encoding natural numbers into lists of @{term "Oc"}s and @{term Bk}s.
   735   encoding natural numbers into lists of @{term "Oc"}s and @{term Bk}s.
   734   % 
   736   % 
   735   \begin{equation}
   737   \begin{equation}
   736   \mbox{\begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   738   \mbox{\begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
   810   @{term "P' \<mapsto> P"} stands for the fact that for all tapes @{term "tp"},
   812   @{term "P' \<mapsto> P"} stands for the fact that for all tapes @{term "tp"},
   811   @{term "P' tp"} implies @{term "P tp"} (similarly for @{text "Q"} and @{text "Q'"}).
   813   @{term "P' tp"} implies @{term "P tp"} (similarly for @{text "Q"} and @{text "Q'"}).
   812 
   814 
   813   Like Asperti and Ricciotti with their notion of realisability, we
   815   Like Asperti and Ricciotti with their notion of realisability, we
   814   have set up our Hoare-rules so that we can deal explicitly
   816   have set up our Hoare-rules so that we can deal explicitly
   815   with total correctness and non-terminantion, rather than have
   817   with total correctness and non-termination, rather than have
   816   notions for partial correctness and termination. Although the latter
   818   notions for partial correctness and termination. Although the latter
   817   would allow us to reason more uniformly (only using Hoare-triples),
   819   would allow us to reason more uniformly (only using Hoare-triples),
   818   we prefer our definitions because we can derive below some simple
   820   we prefer our definitions because we can derive below some simple
   819   Hoare-rules for sequentially composed Turing programs.  In this way
   821   Hoare-rules for sequentially composed Turing programs.  In this way
   820   we can reason about the correctness of @{term "tcopy_begin"}, for
   822   we can reason about the correctness of @{term "tcopy_begin"}, for
   821   example, completely separately from @{term "tcopy_loop"} and @{term
   823   example, completely separately from @{term "tcopy_loop"} and @{term
   822   "tcopy_end"}.
   824   "tcopy_end"}.
   823 
   825 
   824   It is realatively straightforward to prove that the Turing program 
   826   It is relatively straightforward to prove that the Turing program 
   825   @{term "dither"} shown in \eqref{dither} is correct. This program
   827   @{term "dither"} shown in \eqref{dither} is correct. This program
   826   should be the ``identity'' when started with a standard tape representing 
   828   should be the ``identity'' when started with a standard tape representing 
   827   @{text "1"} but loops when started with the @{text 0}-representation instead, as pictured 
   829   @{text "1"} but loops when started with the @{text 0}-representation instead, as pictured 
   828   below.
   830   below.
   829 
   831 
   984   We next need to show that @{term "tcopy_begin"} terminates. For this
   986   We next need to show that @{term "tcopy_begin"} terminates. For this
   985   we introduce lexicographically ordered pairs @{term "(n, m)"}
   987   we introduce lexicographically ordered pairs @{term "(n, m)"}
   986   derived from configurations @{text "(s, (l, r))"} whereby @{text n} is
   988   derived from configurations @{text "(s, (l, r))"} whereby @{text n} is
   987   the state @{text s}, but ordered according to how @{term tcopy_begin} executes
   989   the state @{text s}, but ordered according to how @{term tcopy_begin} executes
   988   them, that is @{text "1 > 2 > 3 > 4 > 0"}; in order to have
   990   them, that is @{text "1 > 2 > 3 > 4 > 0"}; in order to have
   989   a strictly decreasing meansure, @{term m} takes the data on the tape into
   991   a strictly decreasing measure, @{term m} takes the data on the tape into
   990   account and is calculated according to the following measure function:
   992   account and is calculated according to the following measure function:
   991 
   993 
   992   \begin{center}
   994   \begin{center}
   993   \begin{tabular}{rcl}
   995   \begin{tabular}{rcl}
   994   @{term measure_begin_step}@{text "(s, (l, r))"} & @{text "\<equiv>"} & 
   996   @{term measure_begin_step}@{text "(s, (l, r))"} & @{text "\<equiv>"} & 
  1031   That means if we start with a tape of the form @{term "([], <n::nat>)"} then 
  1033   That means if we start with a tape of the form @{term "([], <n::nat>)"} then 
  1032   @{term tcopy} will halt with the tape \mbox{@{term "([Bk], <(n::nat, n::nat)>)"}}, as desired.
  1034   @{term tcopy} will halt with the tape \mbox{@{term "([Bk], <(n::nat, n::nat)>)"}}, as desired.
  1033 
  1035 
  1034   Finally, we are in the position to prove the undecidability of the halting problem.
  1036   Finally, we are in the position to prove the undecidability of the halting problem.
  1035   A program @{term p} started with a standard tape containing the (encoded) numbers
  1037   A program @{term p} started with a standard tape containing the (encoded) numbers
  1036   @{term ns} will \emph{halt} with a standard tape containging a single (encoded) 
  1038   @{term ns} will \emph{halt} with a standard tape containing a single (encoded) 
  1037   number is defined as
  1039   number is defined as
  1038 
  1040 
  1039   \begin{center}
  1041   \begin{center}
  1040   @{thm haltP_def}
  1042   @{thm haltP_def}
  1041   \end{center}
  1043   \end{center}
  1131   \end{center}
  1133   \end{center}
  1132 
  1134 
  1133   \noindent
  1135   \noindent
  1134   This time the Hoare-triple states that @{term tcontra} terminates 
  1136   This time the Hoare-triple states that @{term tcontra} terminates 
  1135   with the ``output'' @{term "<(1::nat)>"}. In both case we come
  1137   with the ``output'' @{term "<(1::nat)>"}. In both case we come
  1136   to a contradiction, which means we have to abondon our assumption 
  1138   to a contradiction, which means we have to abandon our assumption 
  1137   that there exists a Turing machine @{term H} which can in general decide 
  1139   that there exists a Turing machine @{term H} which can in general decide 
  1138   whether Turing machines terminate.
  1140   whether Turing machines terminate.
  1139 *}
  1141 *}
  1140 
  1142 
  1141 
  1143 
  1300 
  1302 
  1301   \noindent 
  1303   \noindent 
  1302   where @{text n} indicates the function expects @{term n} arguments
  1304   where @{text n} indicates the function expects @{term n} arguments
  1303   (@{text z} and @{term s} expect one argument), and @{text rs} stands
  1305   (@{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 
  1306   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  
  1307   the arity, say @{term l}, we can define an inductive evaluation relation that  
  1306   relates a recursive function and a list of natural numbers of length @{text n},
  1308   relates a recursive function @{text r} and a list @{term ns} of natural numbers of length @{text l},
  1307   to what the result of the recurisve function is---we omit the straightforward
  1309   to what the result of the recursive function is, say @{text n}---we omit the straightforward
  1308   definition. Because of space reasons, we also omit the definition of translating
  1310   definition of @{term "rec_cal_rel r ns n"}. Because of space reasons, we also omit the 
  1309   recursive functions into abacus programs and the also the definition of the
  1311   definition of translating
       
  1312   recursive functions into abacus programs. We can prove the following
       
  1313   theorem about the translation: Assuming 
       
  1314   @{thm (prem 1) recursive_compile_to_tm_correct3[where recf="r" and args="ns" and r="n"]}
       
  1315   then the following Hoare-triple holds
       
  1316 
       
  1317   \begin{center}
       
  1318   @{thm (concl) recursive_compile_to_tm_correct3[where recf="r" and args="ns" and r="n"]}
       
  1319   \end{center}
       
  1320 
       
  1321   \noindent
       
  1322   which means that if the recursive function @{text r} with arguments @{text ns} evaluates
       
  1323   to @{text n}, then the corresponding Turing machine @{term "tm_of_rec r"} if started
       
  1324   with the standard tape @{term "([Bk, Bk], <ns::nat list>)"} will terminate
       
  1325   with the standard tape @{term "(Bk \<up> l, <n::nat> @ Bk \<up> m)"} for some @{text l} and @{text m}.
       
  1326 
       
  1327   and the also the definition of the
  1310   universal function (we refer the reader to our formalisation).
  1328   universal function (we refer the reader to our formalisation).
  1311   
  1329   
  1312 *}
  1330 *}
  1313 
  1331 
  1314 (*
  1332 (*
  1330   precisely as Boolos et al present it, but use definitions that make
  1348   precisely as Boolos et al present it, but use definitions that make
  1331   mechanised proofs manageable. For example our definition of the 
  1349   mechanised proofs manageable. For example our definition of the 
  1332   halting state performing @{term Nop}-operations seems to be non-standard, 
  1350   halting state performing @{term Nop}-operations seems to be non-standard, 
  1333   but very much suited to a formalisation in a theorem prover where
  1351   but very much suited to a formalisation in a theorem prover where
  1334   the @{term steps}-function need to be total. We have found an 
  1352   the @{term steps}-function need to be total. We have found an 
  1335   inconsitency in
  1353   inconsistency in
  1336   Bolos et al's usage of definitions of \ldots 
  1354   Bolos et al's usage of definitions of \ldots 
  1337   Our interest in Turing machines 
  1355   Our interest in Turing machines 
  1338   arose from correctness proofs about algorithms where we were unable
  1356   arose from correctness proofs about algorithms where we were unable
  1339   to formalise arguments about decidability but also undecidability
  1357   to formalise arguments about decidability but also undecidability
  1340   proofs in general (for example Wang's tiling problem \cite{Robinson71}).
  1358   proofs in general (for example Wang's tiling problem \cite{Robinson71}).
  1352   undecidability of the halting problem for Turing machines.  This
  1370   undecidability of the halting problem for Turing machines.  This
  1353   took us around 1500 loc of Isar-proofs, which is just one-and-a-half
  1371   took us around 1500 loc of Isar-proofs, which is just one-and-a-half
  1354   times longer than a mechanised proof pearl about the Myhill-Nerode
  1372   times longer than a mechanised proof pearl about the Myhill-Nerode
  1355   theorem. So our conclusion is it not as daunting as we imagined
  1373   theorem. So our conclusion is it not as daunting as we imagined
  1356   reading the paper by Norrish \cite{Norrish11}. The work involved
  1374   reading the paper by Norrish \cite{Norrish11}. The work involved
  1357   with constructing a universial Turing machine via recursive 
  1375   with constructing a universal Turing machine via recursive 
  1358   functions and abacus machines, on the other hand, is not a
  1376   functions and abacus machines, on the other hand, is not a
  1359   project one wants to undertake too many times (our formalisation
  1377   project one wants to undertake too many times (our formalisation
  1360   of abacus machines and their correct translation is approximately 
  1378   of abacus machines and their correct translation is approximately 
  1361   4300 loc; \ldots)
  1379   4300 loc; \ldots)
  1362   
  1380   
  1364   machines by Asperti and Ricciotti \cite{AspertiRicciotti12} in the
  1382   machines by Asperti and Ricciotti \cite{AspertiRicciotti12} in the
  1365   Matita theorem prover. It turns out that their notion of
  1383   Matita theorem prover. It turns out that their notion of
  1366   realisability and our Hoare-triples are very similar, however we
  1384   realisability and our Hoare-triples are very similar, however we
  1367   differ in some basic definitions for Turing machines. Asperti and
  1385   differ in some basic definitions for Turing machines. Asperti and
  1368   Ricciotti are interested in providing a mechanised foundation for
  1386   Ricciotti are interested in providing a mechanised foundation for
  1369   complexity theory. They formalised a universial Turing machine
  1387   complexity theory. They formalised a universal Turing machine
  1370   (which differs from ours by using a more general alphabet), but did
  1388   (which differs from ours by using a more general alphabet), but did
  1371   not describe an undecidability proof. Given their definitions and
  1389   not describe an undecidability proof. Given their definitions and
  1372   infrastructure, we expect this should not be too difficult for them.
  1390   infrastructure, we expect this should not be too difficult for them.
  1373   
  1391   
  1374   For us the most interesting aspect of our work are the correctness 
  1392   For us the most interesting aspect of our work are the correctness 
  1375   proofs for some Turing machines. Informal presentation of computability
  1393   proofs for some Turing machines. Informal presentation of computability
  1376   theory often leave the constructions of particular Turing machines
  1394   theory often leave the constructions of particular Turing machines
  1377   as excercise to the reader, as \cite{Boolos87} for example, deeming it 
  1395   as exercise to the reader, as \cite{Boolos87} for example, deeming it 
  1378   too easy for wasting space. However, as far as we are aware all
  1396   too easy for wasting space. However, as far as we are aware all
  1379   informal presentation leave out any correctness proofs---do the 
  1397   informal presentation leave out any correctness proofs---do the 
  1380   Turing machines really perform the task they are supposed to do. 
  1398   Turing machines really perform the task they are supposed to do. 
  1381   This means we have to find appropriate invariants and measures
  1399   This means we have to find appropriate invariants and measures
  1382   that can be establised for showing correctness and termination.
  1400   that can be established for showing correctness and termination.
  1383   Whenever we can use Hoare-style reasoning, the invariants are
  1401   Whenever we can use Hoare-style reasoning, the invariants are
  1384   relatively straightforward and much smaller than for example 
  1402   relatively straightforward and much smaller than for example 
  1385   the invariants by Myreen for a correctness proof of a garbage
  1403   the invariants by Myreen for a correctness proof of a garbage
  1386   collector \cite[Page 76]{}. The invariant needed for the abacus proof,
  1404   collector \cite[Page 76]{}. The invariant needed for the abacus proof,
  1387   where Hoare-style reasoning does not work, is similar in size as 
  1405   where Hoare-style reasoning does not work, is similar in size as 
  1388   the one by Myreen and finding a sufficiently strong one took
  1406   the one by Myreen and finding a sufficiently strong one took
  1389   us, like Myreen, something on the magnitute of weeks. 
  1407   us, like Myreen, something on the magnitude of weeks. 
  1390 
  1408 
  1391   Our reasoning about the invariants is also not very much 
  1409   Our reasoning about the invariants is also not very much 
  1392   supported by the automation in Isabelle. There is however a tantalising 
  1410   supported by the automation in Isabelle. There is however a tantalising 
  1393   connection between our work and recent work \cite{Jensen13}
  1411   connection between our work and recent work \cite{Jensen13}
  1394   on verifying X86 assembly code. They observed a similar phenomenon
  1412   on verifying X86 assembly code. They observed a similar phenomenon
  1395   with assmebly programs that Hoare-style reasoning is sometimes
  1413   with assembly programs that Hoare-style reasoning is sometimes
  1396   possible, but sometimes not. In order to ease their reasoning they
  1414   possible, but sometimes not. In order to ease their reasoning they
  1397   introduced a more primitive specification logic, on which
  1415   introduced a more primitive specification logic, on which
  1398   for special cases Hoare-rules can be provided.
  1416   for special cases Hoare-rules can be provided.
  1399   It remains to be seen whether their specification logic
  1417   It remains to be seen whether their specification logic
  1400   for assmebly code can make it easier to reason about our Turing
  1418   for assembly code can make it easier to reason about our Turing
  1401   programs. That would be an attractive result, because Turing 
  1419   programs. That would be an attractive result, because Turing 
  1402   machine programs are 
  1420   machine programs are 
  1403 
  1421 
  1404   The code of our formalisation is available from the Mercurial repository at
  1422   The code of our formalisation is available from the Mercurial repository at
  1405   \url{http://www.dcs.kcl.ac.uk/staff/urbanc/cgi-bin/repos.cgi/tm/}
  1423   \url{http://www.dcs.kcl.ac.uk/staff/urbanc/cgi-bin/repos.cgi/tm/}