Paper/Paper.thy
changeset 284 a21fb87bb0bd
parent 239 ac3309722536
equal deleted inserted replaced
283:7d29c3c09bea 284:a21fb87bb0bd
    71   exponent ("_\<^bsup>_\<^esup>") and
    71   exponent ("_\<^bsup>_\<^esup>") and
    72   tcopy ("copy") and 
    72   tcopy ("copy") and 
    73   tape_of ("\<langle>_\<rangle>") and 
    73   tape_of ("\<langle>_\<rangle>") and 
    74   tm_comp ("_ ; _") and 
    74   tm_comp ("_ ; _") and 
    75   DUMMY  ("\<^raw:\mbox{$\_\!\_\,$}>") and
    75   DUMMY  ("\<^raw:\mbox{$\_\!\_\,$}>") and
    76   inv_begin0 ("I\<^isub>0") and
    76   inv_begin0 ("I\<^sub>0") and
    77   inv_begin1 ("I\<^isub>1") and
    77   inv_begin1 ("I\<^sub>1") and
    78   inv_begin2 ("I\<^isub>2") and
    78   inv_begin2 ("I\<^sub>2") and
    79   inv_begin3 ("I\<^isub>3") and
    79   inv_begin3 ("I\<^sub>3") and
    80   inv_begin4 ("I\<^isub>4") and 
    80   inv_begin4 ("I\<^sub>4") and 
    81   inv_begin ("I\<^bsub>cbegin\<^esub>") and
    81   inv_begin ("I\<^bsub>cbegin\<^esub>") and
    82   inv_loop1 ("J\<^isub>1") and
    82   inv_loop1 ("J\<^sub>1") and
    83   inv_loop0 ("J\<^isub>0") and
    83   inv_loop0 ("J\<^sub>0") and
    84   inv_end1 ("K\<^isub>1") and
    84   inv_end1 ("K\<^sub>1") and
    85   inv_end0 ("K\<^isub>0") and
    85   inv_end0 ("K\<^sub>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") and
    88   findnth ("find'_nth") and
    89   recf.id ("id\<^raw:\makebox[0mm]{\,\,\,\,>\<^isup>_\<^raw:}>\<^isub>_") and
    89   recf.id ("id\<^raw:\makebox[0mm]{\,\,\,\,>\<^sup>_\<^raw:}>\<^sub>_") and
    90   Pr ("Pr\<^isup>_") and
    90   Pr ("Pr\<^sup>_") and
    91   Cn ("Cn\<^isup>_") and
    91   Cn ("Cn\<^sup>_") and
    92   Mn ("Mn\<^isup>_") and
    92   Mn ("Mn\<^sup>_") and
    93   rec_exec ("eval") and
    93   rec_exec ("eval") and
    94   tm_of_rec ("translate") and
    94   tm_of_rec ("translate") and
    95   listsum ("\<Sigma>")
    95   listsum ("\<Sigma>")
    96 
    96 
    97  
    97  
   143 apply(case_tac ns)
   143 apply(case_tac ns)
   144 apply(auto simp add: tape_of_nat_pair tape_of_nat_abv)
   144 apply(auto simp add: tape_of_nat_pair tape_of_nat_abv)
   145 done 
   145 done 
   146 
   146 
   147 lemmas HR1 = 
   147 lemmas HR1 = 
   148   Hoare_plus_halt[where ?S.0="R\<iota>" and ?A="p\<^isub>1" and B="p\<^isub>2"]
   148   Hoare_plus_halt[where ?S.0="R\<iota>" and ?A="p\<^sub>1" and B="p\<^sub>2"]
   149 
   149 
   150 lemmas HR2 =
   150 lemmas HR2 =
   151   Hoare_plus_unhalt[where ?A="p\<^isub>1" and B="p\<^isub>2"]
   151   Hoare_plus_unhalt[where ?A="p\<^sub>1" and B="p\<^sub>2"]
   152 
   152 
   153 lemma inv_begin01:
   153 lemma inv_begin01:
   154   assumes "n > 1"
   154   assumes "n > 1"
   155   shows "inv_begin0 n (l, r) = (n > 1 \<and> (l, r) = (Oc \<up> (n - 2), [Oc, Oc, Bk, Oc]))"
   155   shows "inv_begin0 n (l, r) = (n > 1 \<and> (l, r) = (Oc \<up> (n - 2), [Oc, Oc, Bk, Oc]))"
   156 using assms by auto                          
   156 using assms by auto                          
   643   @{thm (lhs) nats2tape(3)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(3)}
   643   @{thm (lhs) nats2tape(3)} & @{text "\<equiv>"} & @{thm (rhs) nats2tape(3)}
   644   \end{tabular}}\label{standard}
   644   \end{tabular}}\label{standard}
   645   \end{equation}
   645   \end{equation}
   646   %
   646   %
   647   \noindent
   647   \noindent
   648   A \emph{standard tape} is then of the form @{text "(Bk\<^isup>k,\<langle>[n\<^isub>1,...,n\<^isub>m]\<rangle> @ Bk\<^isup>l)"} for some @{text k}, 
   648   A \emph{standard tape} is then of the form @{text "(Bk\<^sup>k,\<langle>[n\<^sub>1,...,n\<^sub>m]\<rangle> @ Bk\<^sup>l)"} for some @{text k}, 
   649   @{text l} 
   649   @{text l} 
   650   and @{text "n\<^bsub>1...m\<^esub>"}. Note that the head in a standard tape ``points'' to the 
   650   and @{text "n\<^bsub>1...m\<^esub>"}. Note that the head in a standard tape ``points'' to the 
   651   leftmost @{term "Oc"} on the tape. Note also that the natural number @{text 0} 
   651   leftmost @{term "Oc"} on the tape. Note also that the natural number @{text 0} 
   652   is represented by a single filled cell on a standard tape, @{text 1} by two filled cells and so on.
   652   is represented by a single filled cell on a standard tape, @{text 1} by two filled cells and so on.
   653 
   653 
   668   thus moving all ``regular'' states to the segment starting at @{text
   668   thus moving all ``regular'' states to the segment starting at @{text
   669   n}; the second adds @{term "Suc(length p div 2)"} to the @{text
   669   n}; the second adds @{term "Suc(length p div 2)"} to the @{text
   670   0}-state, thus redirecting all references to the ``halting state''
   670   0}-state, thus redirecting all references to the ``halting state''
   671   to the first state after the program @{text p}.  With these two
   671   to the first state after the program @{text p}.  With these two
   672   functions in place, we can define the \emph{sequential composition}
   672   functions in place, we can define the \emph{sequential composition}
   673   of two Turing machine programs @{text "p\<^isub>1"} and @{text "p\<^isub>2"} as
   673   of two Turing machine programs @{text "p\<^sub>1"} and @{text "p\<^sub>2"} as
   674 
   674 
   675   \begin{center}
   675   \begin{center}
   676   @{thm tm_comp.simps[where ?p1.0="p\<^isub>1" and ?p2.0="p\<^isub>2", THEN eq_reflection]}
   676   @{thm tm_comp.simps[where ?p1.0="p\<^sub>1" and ?p2.0="p\<^sub>2", THEN eq_reflection]}
   677   \end{center}
   677   \end{center}
   678 
   678 
   679   \noindent
   679   \noindent
   680   %This means @{text "p\<^isub>1"} is executed first. Whenever it originally
   680   %This means @{text "p\<^sub>1"} is executed first. Whenever it originally
   681   %transitioned to the @{text 0}-state, it will in the composed program transition to the starting
   681   %transitioned to the @{text 0}-state, it will in the composed program transition to the starting
   682   %state of @{text "p\<^isub>2"} instead. All the states of @{text "p\<^isub>2"}
   682   %state of @{text "p\<^sub>2"} instead. All the states of @{text "p\<^sub>2"}
   683   %have been shifted in order to make sure that the states of the composed 
   683   %have been shifted in order to make sure that the states of the composed 
   684   %program @{text "p\<^isub>1 \<oplus> p\<^isub>2"} still only ``occupy'' 
   684   %program @{text "p\<^sub>1 \<oplus> p\<^sub>2"} still only ``occupy'' 
   685   %an initial segment of the natural numbers.
   685   %an initial segment of the natural numbers.
   686   
   686   
   687   \begin{figure}[t]
   687   \begin{figure}[t]
   688   \begin{center}
   688   \begin{center}
   689   \begin{tabular}{@ {}c@ {\hspace{3mm}}c@ {\hspace{3mm}}c}
   689   \begin{tabular}{@ {}c@ {\hspace{3mm}}c@ {\hspace{3mm}}c}
   968   The first corresponds to the usual Hoare-rule for composition of two
   968   The first corresponds to the usual Hoare-rule for composition of two
   969   terminating programs. The second rule gives the conditions for when
   969   terminating programs. The second rule gives the conditions for when
   970   the first program terminates generating a tape for which the second
   970   the first program terminates generating a tape for which the second
   971   program loops.  The side-conditions about @{thm (prem 3) HR2} are
   971   program loops.  The side-conditions about @{thm (prem 3) HR2} are
   972   needed in order to ensure that the redirection of the halting and
   972   needed in order to ensure that the redirection of the halting and
   973   initial state in @{term "p\<^isub>1"} and @{term "p\<^isub>2"}, respectively, match
   973   initial state in @{term "p\<^sub>1"} and @{term "p\<^sub>2"}, respectively, match
   974   up correctly.  These Hoare-rules allow us to prove the correctness
   974   up correctly.  These Hoare-rules allow us to prove the correctness
   975   of @{term tcopy} by considering the correctness of the components
   975   of @{term tcopy} by considering the correctness of the components
   976   @{term "tcopy_begin"}, @{term "tcopy_loop"} and @{term "tcopy_end"}
   976   @{term "tcopy_begin"}, @{term "tcopy_loop"} and @{term "tcopy_end"}
   977   in isolation. This simplifies the reasoning considerably, for
   977   in isolation. This simplifies the reasoning considerably, for
   978   example when designing decreasing measures for proving the termination
   978   example when designing decreasing measures for proving the termination
  1126   \begin{center}
  1126   \begin{center}
  1127   @{thm tcontra_def}
  1127   @{thm tcontra_def}
  1128   \end{center}
  1128   \end{center}
  1129 
  1129 
  1130   \noindent
  1130   \noindent
  1131   Suppose @{thm (prem 1) "tcontra_halt"} holds. Given the invariants @{text "P\<^isub>1"}\ldots@{text "P\<^isub>3"} 
  1131   Suppose @{thm (prem 1) "tcontra_halt"} holds. Given the invariants @{text "P\<^sub>1"}\ldots@{text "P\<^sub>3"} 
  1132   shown on the 
  1132   shown on the 
  1133   left, we can derive the following Hoare-pair for @{term tcontra} on the right.
  1133   left, we can derive the following Hoare-pair for @{term tcontra} on the right.
  1134 
  1134 
  1135   \begin{center}\small
  1135   \begin{center}\small
  1136   \begin{tabular}{@ {}c@ {\hspace{-10mm}}c@ {}}
  1136   \begin{tabular}{@ {}c@ {\hspace{-10mm}}c@ {}}
  1137   \begin{tabular}[t]{@ {}l@ {}}
  1137   \begin{tabular}[t]{@ {}l@ {}}
  1138   @{term "P\<^isub>1 \<equiv> \<lambda>tp. tp = ([]::cell list, <code_tcontra>)"}\\
  1138   @{term "P\<^sub>1 \<equiv> \<lambda>tp. tp = ([]::cell list, <code_tcontra>)"}\\
  1139   @{term "P\<^isub>2 \<equiv> \<lambda>tp. tp = ([Bk], <(code_tcontra, code_tcontra)>)"}\\
  1139   @{term "P\<^sub>2 \<equiv> \<lambda>tp. tp = ([Bk], <(code_tcontra, code_tcontra)>)"}\\
  1140   @{term "P\<^isub>3 \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>)"}
  1140   @{term "P\<^sub>3 \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <0::nat>)"}
  1141   \end{tabular}
  1141   \end{tabular}
  1142   &
  1142   &
  1143   \begin{tabular}[b]{@ {}l@ {}}
  1143   \begin{tabular}[b]{@ {}l@ {}}
  1144   \raisebox{-20mm}{$\inferrule*{
  1144   \raisebox{-20mm}{$\inferrule*{
  1145     \inferrule*{@{term "{P\<^isub>1} tcopy {P\<^isub>2}"} \\ @{term "{P\<^isub>2} H {P\<^isub>3}"}}
  1145     \inferrule*{@{term "{P\<^sub>1} tcopy {P\<^sub>2}"} \\ @{term "{P\<^sub>2} H {P\<^sub>3}"}}
  1146     {@{term "{P\<^isub>1} (tcopy |+| H) {P\<^isub>3}"}}
  1146     {@{term "{P\<^sub>1} (tcopy |+| H) {P\<^sub>3}"}}
  1147     \\ @{term "{P\<^isub>3} dither \<up>"}
  1147     \\ @{term "{P\<^sub>3} dither \<up>"}
  1148    }
  1148    }
  1149    {@{term "{P\<^isub>1} tcontra \<up>"}}
  1149    {@{term "{P\<^sub>1} tcontra \<up>"}}
  1150   $}
  1150   $}
  1151   \end{tabular}
  1151   \end{tabular}
  1152   \end{tabular}
  1152   \end{tabular}
  1153   \end{center}
  1153   \end{center}
  1154 
  1154 
  1155   \noindent
  1155   \noindent
  1156   This Hoare-pair contradicts our assumption that @{term tcontra} started
  1156   This Hoare-pair contradicts our assumption that @{term tcontra} started
  1157   with @{term "<(code tcontra)>"} halts.
  1157   with @{term "<(code tcontra)>"} halts.
  1158 
  1158 
  1159   Suppose @{thm (prem 1) "tcontra_unhalt"} holds. Again, given the invariants 
  1159   Suppose @{thm (prem 1) "tcontra_unhalt"} holds. Again, given the invariants 
  1160   @{text "Q\<^isub>1"}\ldots@{text "Q\<^isub>3"}
  1160   @{text "Q\<^sub>1"}\ldots@{text "Q\<^sub>3"}
  1161   shown on the 
  1161   shown on the 
  1162   left, we can derive the Hoare-triple for @{term tcontra} on the right.
  1162   left, we can derive the Hoare-triple for @{term tcontra} on the right.
  1163 
  1163 
  1164   \begin{center}\small
  1164   \begin{center}\small
  1165   \begin{tabular}{@ {}c@ {\hspace{-18mm}}c@ {}}
  1165   \begin{tabular}{@ {}c@ {\hspace{-18mm}}c@ {}}
  1166   \begin{tabular}[t]{@ {}l@ {}}
  1166   \begin{tabular}[t]{@ {}l@ {}}
  1167   @{term "Q\<^isub>1 \<equiv> \<lambda>tp. tp = ([]::cell list, <code_tcontra>)"}\\
  1167   @{term "Q\<^sub>1 \<equiv> \<lambda>tp. tp = ([]::cell list, <code_tcontra>)"}\\
  1168   @{term "Q\<^isub>2 \<equiv> \<lambda>tp. tp = ([Bk], <(code_tcontra, code_tcontra)>)"}\\
  1168   @{term "Q\<^sub>2 \<equiv> \<lambda>tp. tp = ([Bk], <(code_tcontra, code_tcontra)>)"}\\
  1169   @{term "Q\<^isub>3 \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>)"}
  1169   @{term "Q\<^sub>3 \<equiv> \<lambda>tp. \<exists>k. tp = (Bk \<up> k, <1::nat>)"}
  1170   \end{tabular}
  1170   \end{tabular}
  1171   &
  1171   &
  1172   \begin{tabular}[t]{@ {}l@ {}}
  1172   \begin{tabular}[t]{@ {}l@ {}}
  1173   \raisebox{-20mm}{$\inferrule*{
  1173   \raisebox{-20mm}{$\inferrule*{
  1174     \inferrule*{@{term "{Q\<^isub>1} tcopy {Q\<^isub>2}"} \\ @{term "{Q\<^isub>2} H {Q\<^isub>3}"}}
  1174     \inferrule*{@{term "{Q\<^sub>1} tcopy {Q\<^sub>2}"} \\ @{term "{Q\<^sub>2} H {Q\<^sub>3}"}}
  1175     {@{term "{Q\<^isub>1} (tcopy |+| H) {Q\<^isub>3}"}}
  1175     {@{term "{Q\<^sub>1} (tcopy |+| H) {Q\<^sub>3}"}}
  1176     \\ @{term "{Q\<^isub>3} dither {Q\<^isub>3}"}
  1176     \\ @{term "{Q\<^sub>3} dither {Q\<^sub>3}"}
  1177    }
  1177    }
  1178    {@{term "{Q\<^isub>1} tcontra {Q\<^isub>3}"}}
  1178    {@{term "{Q\<^sub>1} tcontra {Q\<^sub>3}"}}
  1179   $}
  1179   $}
  1180   \end{tabular}
  1180   \end{tabular}
  1181   \end{tabular}
  1181   \end{tabular}
  1182   \end{center}
  1182   \end{center}
  1183 
  1183 
  1194 
  1194 
  1195 text {*
  1195 text {*
  1196   \noindent
  1196   \noindent
  1197   Boolos et al \cite{Boolos87} use abacus machines as a stepping stone
  1197   Boolos et al \cite{Boolos87} use abacus machines as a stepping stone
  1198   for making it less laborious to write Turing machine
  1198   for making it less laborious to write Turing machine
  1199   programs. Abacus machines operate over a set of registers @{text "R\<^isub>0"},
  1199   programs. Abacus machines operate over a set of registers @{text "R\<^sub>0"},
  1200   @{text "R\<^isub>1"}, \ldots{}, @{text "R\<^isub>n"} each being able to hold an arbitrary large natural
  1200   @{text "R\<^sub>1"}, \ldots{}, @{text "R\<^sub>n"} each being able to hold an arbitrary large natural
  1201   number.  We use natural numbers to refer to registers; we also use a natural number
  1201   number.  We use natural numbers to refer to registers; we also use a natural number
  1202   to represent a program counter and to represent jumping ``addresses'', for which we 
  1202   to represent a program counter and to represent jumping ``addresses'', for which we 
  1203   use the letter @{text l}. An abacus 
  1203   use the letter @{text l}. An abacus 
  1204   program is a list of \emph{instructions} defined by the datatype:
  1204   program is a list of \emph{instructions} defined by the datatype:
  1205 
  1205 
  1352             & @{text "|"}   & @{term s} & (successor-function)\\
  1352             & @{text "|"}   & @{term s} & (successor-function)\\
  1353             & @{text "|"}   & @{term "id n m"} & (projection)\\
  1353             & @{text "|"}   & @{term "id n m"} & (projection)\\
  1354   \end{tabular} &
  1354   \end{tabular} &
  1355   \begin{tabular}{cl@ {\hspace{4mm}}l}
  1355   \begin{tabular}{cl@ {\hspace{4mm}}l}
  1356   @{text "|"} & @{term "Cn n f fs"} & (composition)\\
  1356   @{text "|"} & @{term "Cn n f fs"} & (composition)\\
  1357   @{text "|"} & @{term "Pr n f\<^isub>1 f\<^isub>2"} & (primitive recursion)\\
  1357   @{text "|"} & @{term "Pr n f\<^sub>1 f\<^sub>2"} & (primitive recursion)\\
  1358   @{text "|"} & @{term "Mn n f"} & (minimisation)\\
  1358   @{text "|"} & @{term "Mn n f"} & (minimisation)\\
  1359   \end{tabular}
  1359   \end{tabular}
  1360   \end{tabular}
  1360   \end{tabular}
  1361   \end{center}
  1361   \end{center}
  1362 
  1362